avatar

目录
反射机制reflect

参考:【itheima】

[toc]

反射机制reflect

概述

  1. Java反射机制 通过Reflection APIs 取得任何已知名称的class的内部消息
  2. Java反射机制容许程序在运行时加载、探知、使用编译期间完全未知的 classes。
  3. 换言之,Java 可以加载一个运行时才得知名称的 class,获得其完整结构。

代码实现

加载类的3种方式

java
1
2
3
4
5
6
7
8
9
10
@Test
public void fun1() throws Exception{
//1.
Class clazz1 = Class.forName("m.reflect.Student");//把硬盘的类加载到内存,封装成Class类
//2.
Class clazz2 = new Student().getClass();
//3.
Class clazz3 = Student.class;

}

反射 类的构造函数 并 创建类对象

java
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
55
56
/*
* 获取类【无参构造函数】Student()》创建类对象
*/
@Test
public void fun2() throws Exception {

Class clazz = Class.forName("m.reflect.Student");

Constructor c = clazz.getConstructor(null);//取得无参构造函数

Student s = (Student) c.newInstance(null);//创建对象

System.out.println(s.name);
}
/*
* 获取类【有参构造函数】Student(String name)
*/
@Test
public void fun3() throws Exception {

Class clazz = Class.forName("m.reflect.Student");

Constructor c = clazz.getConstructor(String.class);//取得有参构造函数

Student s = (Student) c.newInstance("www");

System.out.println(s.name);//打印mm
}
/*
* 获取类【多参数构造函数】:Student(String name,int pass)
*/
@Test
public void fun4() throws Exception {

Class clazz = Class.forName("m.reflect.Student");

Constructor c = clazz.getConstructor(String.class,int.class);

Student s = (Student) c.newInstance("www",3);

System.out.println(s.name);
}
/*
* 获取类【私有构造函数】:private Student(List list)
*/
@Test
public void fun5() throws Exception {

Class clazz = Class.forName("m.reflect.Student");

Constructor c = clazz.getDeclaredConstructor(List.class);//私有构造函数
c.setAccessible(true);//暴力反射,强制打开访问权限
Student s = (Student) c.newInstance(new ArrayList());

System.out.println(s.name);
}

反射类里的方法

java
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
55
56
57
58
59
60
61
62
63
64
65
66
@Test
public void fun7() throws Exception{

Student p=new Student();

Class clazz = Class.forName("m.reflect.Student");

Method method = clazz.getMethod("method1",null);//取得方法

method.invoke(p, null);//执行方法,非静态方法需要一个实例化对象
}
/*
* 反射类里的方法:method1(String name ,int a)
*/
@Test
public void fun8() throws Exception{

Student p=new Student();

Class clazz = Class.forName("m.reflect.Student");

Method method = clazz.getMethod("method1",String.class,int.class);

method.invoke(p, "MA",30);
}
/*
* 反射类里的方法:method1(String name ,int[] a)
*/
@Test
public void fun9() throws Exception{

Student p=new Student();

Class clazz = Class.forName("m.reflect.Student");

Method method = clazz.getMethod("method1",String.class,int[].class);

method.invoke(p, "MA",new int[]{1,23});
}
/*
* 反射类里的私有方法:private void method5(String name)
*/
@Test
public void fun10() throws Exception{

Student p=new Student();


Class clazz = Class.forName("m.reflect.Student");

Method method = clazz.getDeclaredMethod("method5",String.class);//取得私有方法
method.setAccessible(true);
method.invoke(p, "MA");
}
/*
* 反射类里的静态方法:public static void method6(){
*/
@Test
public void fun11() throws Exception{

Class clazz = Class.forName("m.reflect.Student");

Method method = clazz.getMethod("method6",String.class);

method.invoke(null, "MA");//静态方法不需要实例化对象
}

反射类里字段(属性)

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void fun12() throws Exception{

Student s = new Student();
Class clazz = Class.forName("m.reflect.Student");

Field field=clazz.getField("name");//得到字段

String name = (String) field.get(s);//得到字段的值(非static属性,需要实例化对象)

Class type = field.getType();//获取字段的类型
System.out.println(name);
System.out.println(type);

field.set(s, "sssss");//为字段赋值
System.out.println(s.name);
}
文章作者: Machine
文章链接: https://machine4869.gitee.io/2018/04/18/15326643428215/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哑舍
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论