Java反射Reflection--運行時生成instance
想生成對象的實體,在反射動態(tài)機制中有兩種方法,一個針對無變量的構造方法,一個針對帶參數(shù)的構造方法,,如果想調用無參數(shù)的構造函數(shù)直接調用Class類中的newInstance(),而如果想調用有參數(shù)的構造函數(shù),則需要調用Constructor類中newInstance()方法,首先準備一個Class[]作為Constructor的參數(shù)類型。然后調用該Class對象的getConstructor()方法獲得一個專屬的Constructor的對象,最后再準備一個Object[]作為Constructor對象昂的newInstance()方法的實參。
在這里需要說明的是 只有兩個類擁有newInstance()方法,分別是Class類和Constructor類,Class類中的newInstance()方法是不帶參數(shù)的,而Constructro類中的newInstance()方法是帶參數(shù)的需要提供必要的參數(shù)。
下面提供的代碼是構造Customer2類的三個構造函數(shù)
客戶虐我千百遍,我待客戶如初戀!
在這里需要說明的是 只有兩個類擁有newInstance()方法,分別是Class類和Constructor類,Class類中的newInstance()方法是不帶參數(shù)的,而Constructro類中的newInstance()方法是帶參數(shù)的需要提供必要的參數(shù)。
下面提供的代碼是構造Customer2類的三個構造函數(shù)
1
import java.lang.reflect.Constructor;
2
import java.lang.reflect.InvocationTargetException;
3
4
/**
5
* 在反射Reflection機制中,想生成一個類的實例有兩種方法 一個是針對無參數(shù)的構造函數(shù) ,另一個是針對有參數(shù)的構造函數(shù)
6
*
7
*/
8
public class ReflecTest3 {
9
10
/**
11
* 反射的動態(tài)性質之一: 運行期動態(tài)生成Instance
12
*
13
* @throws IllegalAccessException
14
* @throws InstantiationException
15
* @throws NoSuchMethodException
16
* @throws InvocationTargetException
17
* @throws SecurityException
18
* @throws IllegalArgumentException
19
*/
20
public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException {
21
Customer2 customer = new Customer2();
22
Class cls=customer.getClass();
23
// 獲得Class所代表的對象的所有類型的構造函數(shù)Constructor的數(shù)組
24
Constructor ctor[]=cls.getDeclaredConstructors();
25
for(int i=0;i<ctor.length;i++){
26
// 獲得對應的構造函數(shù)參數(shù)列表的Class類型的數(shù)組
27
Class cx[]=ctor[i].getParameterTypes();
28
if(cx.length==0){
29
Object obj=cls.newInstance();
30
System.out.println(obj);
31
}else if(cx.length==2){
32
Customer2 obj=(Customer2)cls.getConstructor(cx).newInstance(new Object[]{new Long(123),"hejianjie"});
33
System.out.println(obj);
34
}else if(cx.length==3){
35
Customer2 obj=(Customer2)cls.getConstructor(cx).newInstance(new Object[]{new Long(133),"China-Boy",new Integer(21)});
36
System.out.println(obj);
37
}
38
}
39
}
40
}
41
42
class Customer2 {
43
44
private Long id;
45
46
private String name;
47
48
private int age;
49
50
/**
51
* 無參數(shù)的構造函數(shù)
52
*
53
*/
54
public Customer2() {
55
56
}
57
58
/**
59
* public修飾的有參數(shù)的構造函數(shù),3個參數(shù)
60
*
61
* @param id
62
* @param name
63
* @param age
64
*/
65
public Customer2(Long id, String name, int age) {
66
this.id = id;
67
this.name = name;
68
this.age = age;
69
}
70
71
/**
72
* public修飾的構造函數(shù),2個參數(shù)
73
*
74
* @param id
75
* @param name
76
*/
77
public Customer2(Long id, String name) {
78
this.id = id;
79
this.name = name;
80
this.age = age;
81
}
82
83
public int getAge() {
84
return age;
85
}
86
87
public void setAge(int age) {
88
this.age = age;
89
}
90
91
public Long getId() {
92
return id;
93
}
94
95
public void setId(Long id) {
96
this.id = id;
97
}
98
99
public String getName() {
100
return name;
101
}
102
103
public void setName(String name) {
104
this.name = name;
105
}
106
107
public String toString() {
108
return ("id==" + this.getId() + " Name==" + this.getName() + " Age:" + this.getAge());
109
}
110
111
}
112

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

客戶虐我千百遍,我待客戶如初戀!
posted on 2008-03-12 17:44 阿南 閱讀(1254) 評論(0) 編輯 收藏 所屬分類: Cool文存檔