forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaReflect.java
More file actions
37 lines (35 loc) · 1020 Bytes
/
JavaReflect.java
File metadata and controls
37 lines (35 loc) · 1020 Bytes
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
package JavaBasic;
import java.lang.reflect.InvocationTargetException;
/**
* @Classname JavaReflect
* @Description
* @Date 19-7-24 下午6:59
* @Created by mao<tianmao818@qq.com>
*/
public class JavaReflect {
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException,IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
//获取类
//第一种
Class clazz1=new Test().getClass();
System.out.println(clazz1);
//第二种
Class clazz2=Test.class;
System.out.println(clazz2);
//第三种
Class clazz3=Class.forName("JavaBasic.Test");
System.out.println(clazz3);
//动态创建对象
//方法1
Test obj1=(Test)clazz1.newInstance();
System.out.println(obj1);
//方法2
Test obj2=(Test)clazz1.getConstructor().newInstance();
System.out.println(obj2);
}
}
class Test{
public Test(){
}
}