国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 色猫猫国产区一区二在线视频 | 成人久 | 国产色爱综合网 | 国产视频综合在线 | 美女一区 | 人人澡人人射 | 日韩精品一区二区三区在线播放 | 在线观看视频黄 | 亚洲精品白浆高清久久久久久 | 国产a区| 美女一级 | 日韩1区 | 91嫩草精品| 欧美一级二级视频 | 久久久综合网 | a天堂在线 | 国产乱叫456 | 精品久久久久久国产 | 久久久.com | 国产免费av在线 | 国产在线看片 | 在线观看日韩av | 欧洲一级毛片 | 日本一区二区精品视频 | 午夜精品一区二区三区在线视频 | 亚洲成av人影片在线观看 | 中文字幕日韩一区二区不卡 | 男人的天堂久久 | 在线一区 | 精品成人免费一区二区在线播放 | 精品国产欧美一区二区三区成人 | 亚洲成年人网址 | 午夜久久久 | 亚洲三级在线 | 色综合久久久久 | 国产欧美高清在线观看 | 日韩精品久久久 | 久草青青草| 视频一区二区三区中文字幕 | 亚洲片国产一区一级在线观看 | 亚洲激情精品 |