Java总结篇系列:Java 反射

java

Java反射:

  1 package com.corn;

2

3 import java.lang.reflect.Constructor;

4 import java.lang.reflect.Field;

5 import java.lang.reflect.InvocationTargetException;

6 import java.lang.reflect.Method;

7

8 public class TestReflect {

9

10 public static void main(String[] args) {

11

12 // 获取“类类型对象” 方法1

13 Class<?> c0 = User.class;

14

15 // 获取“类类型对象” 方法2

16 User user1 = new User();

17 Class<?> c1 = user1.getClass();

18

19 User user2 = new User();

20 Class<?> c2 = user2.getClass();

21

22 System.out.println("user class instance 0:" + c0); //user class instance 0:class com.corn.User

23 System.out.println("user class instance 1:" + c1); //user class instance 1:class com.corn.User

24 System.out.println("user class instance 2:" + c2); //user class instance 2:class com.corn.User

25

26 //c0 == c1 :true, c1 == c2 :true

27 System.out.println("c0 == c1 :" + (c0 == c1) + ", c1 == c2 :" + (c1 == c2));

28

29 try {

30 // 获取“类类型对象” 方法3

31 Class<?> c3 = Class.forName("com.corn.User");

32 System.out.println("user class c3:" + c3); //user class c3:class com.corn.User

33

34 try {

35 User user3 = (User) c3.newInstance();

36

37 //Constructor public com.corn.User(int,java.lang.String,int)

38 //Constructor public com.corn.User(int,java.lang.String)

39 //Constructor public com.corn.User()

40 Constructor<?>[] cons = c3.getConstructors();

41 for (int i = 0; i < cons.length; i++) {

42 // 一次返回类定义中所有public修饰符修饰的“构造器对象”,其与类定义中的构造器顺序不一定相同

43 System.out.println("Constructor " + cons[i]);

44 }

45

46 try {

47 User user4 = (User) cons[1].newInstance(51, "corn");

48 //user instance 4:user instance uid:51 name:corn

49 System.out.println("user instance 4:" + user4);

50

51 // 返回特定参数类型的“构造器对象”所新建的实例

52 User user40;

53 user40 = (User) c3.getConstructor(int.class, String.class).newInstance(520, "corn0");

54 //user instance 40:user instance uid:520 name:corn0

55 System.out.println("user instance 40:" + user40);

56

57 // 返回该类“类类型对象”的所有的父类“类类型对象”

58 Class<?> father = c3.getSuperclass();

59 //father class instance class com.corn.Person

60 System.out.println("father class instance " + father);

61

62 //返回类对象所有的接口对象(其实也是类类型对象)数组

63 Class<?>[] interfaceArray = c3.getInterfaces();

64 //interface class instance 0 interface com.corn.Workable

65 //interface class instance 1 interface com.corn.Thinkable

66 for (int i = 0; i < interfaceArray.length; i++) {

67 // 与implements中接口顺序相同

68 System.out.println("interface class instance " + i + " " + interfaceArray[i]);

69 }

70

71 } catch (IllegalArgumentException e) {

72 e.printStackTrace();

73 } catch (InvocationTargetException e) {

74 e.printStackTrace();

75 }

76

77 } catch (InstantiationException e) {

78 e.printStackTrace();

79 } catch (IllegalAccessException e) {

80 e.printStackTrace();

81 }

82 } catch (ClassNotFoundException e) {

83 e.printStackTrace();

84 } catch (Exception e) {

85 e.printStackTrace();

86 }

87

88 // 返回“类类型对象”所具有的全部属性实例/对象

89 Field[] fieldArray = c0.getDeclaredFields();

90 //filed 0 private int com.corn.User.uid

91 //filed 1 private java.lang.String com.corn.User.name

92 //filed 2 private int com.corn.User.age

93 for (int i = 0; i < fieldArray.length; i++) {

94 System.out.println("filed " + i + " " + fieldArray[i]);

95 }

96

97 User u = new User(12, "yumidi", 99);

98

99 try {

100 // 直接获取private/私有属性的值

101 Field nameField = c0.getDeclaredField("name");

102 //取消Java对访问修饰符的检查

103 nameField.setAccessible(true);

104 String nameValue = (String) nameField.get(u);

105 //name:yumidi

106 System.out.println("name:" + nameValue);

107

108 // 直接改变private/私有属性的值

109 nameField.set(u, "corn");

110 //new name:corn

111 System.out.println("new name:" + u.getName());

112

113 } catch (NoSuchFieldException e) {

114 e.printStackTrace();

115 } catch (SecurityException e) {

116 e.printStackTrace();

117 } catch (Exception e) {

118 e.printStackTrace();

119 }

120

121 //访问私有方法

122 try {

123 Method method = c0.getDeclaredMethod("getResult");

124 //取消Java对访问修饰符的检查

125 method.setAccessible(true);

126 int result = (int) method.invoke(u);

127 //result:1000

128 System.out.println("result:" + result);

129

130 } catch (NoSuchMethodException e) {

131 e.printStackTrace();

132 } catch (SecurityException e) {

133 e.printStackTrace();

134 } catch (Exception e) {

135 e.printStackTrace();

136 }

137

138 }

139 }

140

141 class User extends Person implements Workable, Thinkable {

142

143 private int uid;

144 private String name;

145 private int age;

146

147 public User() {

148

149 }

150

151 private User(int uid) {

152 this.uid = uid;

153 }

154

155 protected User(String name) {

156 this.name = name;

157 }

158

159 public User(int uid, String name) {

160 this.uid = uid;

161 this.name = name;

162 }

163

164 public User(int uid, String name, int age) {

165 this.uid = uid;

166 this.name = name;

167 this.age = age;

168 }

169

170 public int getUid() {

171 return uid;

172 }

173

174 public void setUid(int uid) {

175 this.uid = uid;

176 }

177

178 public String getName() {

179 return name;

180 }

181

182 public void setName(String name) {

183 this.name = name;

184 }

185

186 public int getAge() {

187 return age;

188 }

189

190 public void setAge(int age) {

191 this.age = age;

192 }

193

194 private int getResult() {

195 return 1000;

196 }

197

198 @Override

199 public String toString() {

200 return "user instance uid:" + this.uid + " name:" + this.name;

201 }

202

203 }

204

205 class Person {

206

207 }

208

209 interface Workable {

210

211 }

212

213 interface Thinkable {

214

215 }

以上是 Java总结篇系列:Java 反射 的全部内容, 来源链接: utcz.com/z/391826.html

回到顶部