服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 实例讲解Java基础之反射

实例讲解Java基础之反射

2021-07-19 09:05请赐个名吧 Java教程

今天小编就为大家分享一篇关于实例讲解Java基础之反射,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

前期准备

编写一个真实类phone,实现list接口

?
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class phone implements list {
  public double price;
  public string name;
  public phone() {
  }
  public phone(double price, string name) {
    this.price = price;
    this.name = name;
  }
  public double getprice() {
    return price;
  }
  public void gege(string h){
    system.out.println("gege的"+h);
  }
  public void setprice(double price) {
    this.price = price;
  }
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  @override
  public string tostring() {
    return "phone{" +
        "price=" + price +
        ", name='" + name + '\'' +
        '}';
  }
  @override
  public int size() {
    return 0;
  }
  @override
  public boolean isempty() {
    return false;
  }
  @override
  public boolean contains(object o) {
    return false;
  }
  @override
  public iterator iterator() {
    return null;
  }
  @override
  public object[] toarray() {
    return new object[0];
  }
  @override
  public boolean add(object o) {
    return false;
  }
  @override
  public boolean remove(object o) {
    return false;
  }
  @override
  public boolean addall(collection c) {
    return false;
  }
  @override
  public boolean addall(int index, collection c) {
    return false;
  }
  @override
  public void clear() {
  }
  @override
  public object get(int index) {
    return null;
  }
  @override
  public object set(int index, object element) {
    return null;
  }
  @override
  public void add(int index, object element) {
  }
  @override
  public object remove(int index) {
    return null;
  }
  @override
  public int indexof(object o) {
    return 0;
  }
  @override
  public int lastindexof(object o) {
    return 0;
  }
  @override
  public listiterator listiterator() {
    return null;
  }
  @override
  public listiterator listiterator(int index) {
    return null;
  }
  @override
  public list sublist(int fromindex, int toindex) {
    return null;
  }
  @override
  public boolean retainall(collection c) {
    return false;
  }
  @override
  public boolean removeall(collection c) {
    return false;
  }
  @override
  public boolean containsall(collection c) {
    return false;
  }
  @override
  public object[] toarray(object[] a) {
    return new object[0];
  }
}

1.反射之4种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
public class test2 {
  public static void main(string[] args) throws illegalaccessexception, instantiationexception, classnotfoundexception {
    //第一种
    phone p = new phone(2999,"小米");
    system.out.println(p);//phone{price=2999.0, name='小米'}
    //第二种 需要一个空参构造
    class<phone> phoneclass = phone.class;
    phone phone = phoneclass.newinstance();
    phone.setname("华为");
    phone.setprice(3499);
    system.out.println(phone);//phone{price=3499.0, name='华为'}
    //第三种
    class<?> aclass = class.forname("com.demo.bean.phone");
    phone p2 = (phone) aclass.newinstance();
    p2.setprice(2999);
    p2.setname("魅族");
    system.out.println(p2);//phone{price=2999.0, name='魅族'}
    //第四种,需要一个配置文件phone.properties
    string name = resourcebundle.getbundle("phone").getstring("myphone");
    class<?> bclass = class.forname(name);
    phone p3 = (phone) bclass.newinstance();
    p3.setprice(3299);
    p3.setname("锤子");
    system.out.println(p3);//phone{price=3299.0, name='锤子'}
  }
}

配置文件phone.properties

myphone=com.demo.bean.phone

2. 反射之获取类、父类、实现接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class test3 {
  public static void main(string[] args) throws classnotfoundexception {
    string string = resourcebundle.getbundle("phone").getstring("myphone");
    class<?> aclass = class.forname(string);
    //获取类的完整路径
    system.out.println(aclass.getname());//com.demo.bean.phone
    //获取类的简单名字
    system.out.println(aclass.getsimplename());//phone
    //获取类的父类
    class<?> superclass = aclass.getsuperclass();
    system.out.println(superclass.getname());//java.lang.object
    system.out.println(superclass.getsimplename());//object
    //获得类的接口
    class<?>[] interfaces = aclass.getinterfaces();
    for (class<?> in:interfaces
       ) {
      system.out.println(in.getsimplename());
    }
  }
}

3.反射之获取空参、有参构造

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class test4 {
  public static void main(string[] args) throws classnotfoundexception, illegalaccessexception, instantiationexception, nosuchmethodexception {
    string string = resourcebundle.getbundle("phone").getstring("myphone");
    class<?> aclass = class.forname(string);
    //调用的是无参的构造方法
    phone p1 = (phone) aclass.newinstance();
    p1.setname("华为");
    p1.setprice(2999);//phone{price=2999.0, name='华为'}
    system.out.println(p1);
    //获得无参的构造方法
    constructor<?> constructor = aclass.getconstructor();
    system.out.println(constructor);//public com.demo.bean.phone()
    //获得所有的构造方法
    constructor<?>[] constructors = aclass.getconstructors();
    for (constructor<?> c:constructors
       ) {
      system.out.println(c);
    }
  }
}

4.反射之获取方法

?
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
public class test5 {
  public static void main(string[] args) throws classnotfoundexception, nosuchmethodexception,instantiationexception,illegalaccessexception,invocationtargetexception{
    string string = resourcebundle.getbundle("phone").getstring("myphone");
    class<?> aclass = class.forname(string);
    //包含了父类的方法
    method[] methods = aclass.getmethods();
    for (method m:methods
       ) {
      system.out.println(m);
    }
    //本类中的方法,没有父类的方法
    method[] declaredmethods = aclass.getdeclaredmethods();
    for (method m:declaredmethods
       ) {
      system.out.println(m);
    }
    method gege = aclass.getmethod("gege",string.class);
    //获取gege方法的权限修饰符
    system.out.println(modifier.tostring(gege.getmodifiers()));
    //获取gege方法的返回值类型
    system.out.println(gege.getreturntype());
    //设置gege的参数值
    object o = aclass.newinstance();
    gege.invoke(o,"aa");
  }
}

5.反射之获取字段

?
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
public class test6 {
  public static void main(string[] args) throws classnotfoundexception, nosuchfieldexception, illegalaccessexception, instantiationexception {
    string string = resourcebundle.getbundle("phone").getstring("myphone");
    class<?> aclass = class.forname(string);
    //只能调用public 字段,但是能得到父类的字段
    field[] fields = aclass.getfields();
    for (field f:fields
       ) {
      system.out.println(f.getname());
    }
    //只能调用public 字段,只能得到本类中的字段
    field[] declaredfields = aclass.getdeclaredfields();
    for (field f:declaredfields
       ) {
      system.out.println(f.getname());
    }
    //获取某一字段的数据类型
    field name = aclass.getfield("name");
    string simplename = name.gettype().getsimplename();
    system.out.println(simplename);
    name.setaccessible(true);
    object o = aclass.newinstance();
    name.set(o,"华为");
    system.out.println(name.get(o));
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/qq_34191426/article/details/88141986

延伸 · 阅读

精彩推荐