2013. 7. 25. 16:53

Class Class<T>

java.lang.Object

java.lang.Class<T>

 

 

 Class<T> 클래스는 특정 클래스나 인터페이스에 관한 정보를 검색할 수 있는 메소드를 포함하고 있다.

T는 이 Class 객체에 의해 모델화 되는 클래스의 형태로 예를 들어, String.class 의 형태는 Class<String>이다. 

모델화 되는 클래스가 명확하지 않는 경우 Class<?>를 사용한다.

 

 

● static Class<?> forName(String className) 

 : Returns the Class object associated with the class or interface with the given string name.

● static Class<?> forName(String name, boolean initialize, ClassLoader loader) 

 : Returns the Class object associated with the class or interface with the given string name, using the given class loader.

● T newInstance() : Creates a new instance of the class represented by this Class object.

new 연산자를 사용하지 않고도 원하는 클래스의 객체생성이 가능하다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.test;
  
public class LogicTest {
    public static void main(String[] args) throws Exception {
  
        Temp t = new Temp();
        t.showText();
  
        Temp t2 = (Temp) Class.forName("com.test.Temp").newInstance();
        t2.showText();
         
        Class<?> cls = Class.forName("com.test.Temp");
        Object ob = cls.newInstance();
        Temp t3 = (Temp) ob;
        t3.showText();
    }
}
  
class Temp {
    public void showText() {
        System.out.println("하이");
    }
}
 
 
하이
하이
하이

 

 

다음처럼 JDBC 커넥션 드라이버를 로딩할 때 사용하기도 한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
public static Connection getConnection() {
    String url="jdbc:oracle:thin:@220.76.176.66:1521:orcl", user="noritersand", pwd="java301$!";
    if(conn == null) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    return conn;
}

 

 

 

■ 클래스의 정보 구하기

 

1
2
3
4
5
6
7
8
Class<?> cls = Class.forName("java.lang.String");
System.out.println("클래스가 갖고 있는 메소드");
 
Method[] m = cls.getMethods();
 
for(Method mm : m) {
    System.out.println(mm); //해당클래스의 메소드를 출력할 수 있다.
}

 

 

 

■ Class의 forName()메서드로 객체생성

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.test;
 
import java.lang.reflect.Method;
 
public class LogicTest {
    public static void main(String[] args) throws Exception {
 
        // Demo7 d = new Demo7()
        Class<?> cls = Class.forName("com.test.Demo7");
 
        Object ob = cls.newInstance(); // 객체생성
 
        Demo7 d = (Demo7) ob;
        d.setName("자바");
        d.write();
 
        // 메서드 정의
        Method m1 = cls.getDeclaredMethod("add", new Class[] { int.class, int.class });
        Method m2 = cls.getDeclaredMethod("plus", new Class[] { Integer.class, Integer.class });
        Method m3 = cls.getDeclaredMethod("print", new Class[] { String.class, int.class });
        Method m4 = cls.getDeclaredMethod("write", null);
 
        // 메서드 호출
        Object o1 = m1.invoke(ob, new Object[] { 10, 20 });
        System.out.println(o1);
 
        // 메서드 호출2
        m3.invoke(ob, new Object[] { "합 : ", o1 });
    }
}
 
class Demo7 {
    private String name;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void write() {
        System.out.println(name);
    }
 
    public int add(int a, int b) {
        return a + b;
    }
 
    public Integer plus(Integer a, Integer b) {
        return a + b;
    }
 
    public void print(String title, int a) {
        System.out.println(title + a);
    }
}

 

 

 

'java' 카테고리의 다른 글

Arrays  (0) 2013.07.25
File  (0) 2013.07.25
날짜관련  (0) 2013.07.25
Date, SimpleDateFormat  (0) 2013.07.25
Calendar, GregorianCalendar  (0) 2013.07.25
Posted by 1+1은?