java中反射知识点总结
1 package Demo;
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 import java.lang.reflect.Modifier;
8
9 public class Test {
10
11 public static void main(String[] args) {
12 Class clazz;
13 try {
14 //01:clazz = Class.forName("Demo.Student");
15 //02:clazz=Student.class;
16 clazz=new Student().getClass().getSuperclass();//03:并获取父类
17 System.out.println(clazz);
18 //获取基本数据类型的类 byte short int long float double boolean char
19 clazz=Byte.TYPE;
20 //输出byte
21 System.out.println(clazz);
22 clazz=Short.TYPE;
23 clazz=Integer.TYPE;
24 clazz=Long.TYPE;
25 clazz=Float.TYPE;
26 clazz=Double.TYPE;
27 clazz=Boolean.TYPE;
28 clazz=Character.TYPE;
29
30 //获取基本数据类型本身的类 class java.lang.Byte
31 clazz=Byte.class;
32 System.out.println(clazz);
33 clazz=Short.class;
34 clazz=Integer.class;
35 clazz=Long.class;
36 clazz=Float.class;
37 clazz=Double.class;
38 clazz=Boolean.class;
39 clazz=Character.class;
40
41 //获取数组对应的类 例如下为class java.lang.String
42 String [] name=new String[8];
43 clazz=String[].class;
44 System.out.println(clazz);
45
46
47
48
49 //获取类中的构造方法
50 //获取public级别的构造方法
51 clazz=Class.forName("Demo.Student");
52 //Constructor[] constructors=clazz.getConstructors();
53 /*for (Constructor constructor : constructors) {
54 System.out.println(constructor);
55 }*/
56
57 //获取所有的构造方法
58 Constructor[] constructors=clazz.getDeclaredConstructors();
59 for (Constructor constructor : constructors) {
60 System.out.println(constructor);
61 }
62
63 //获取单个public级别的构造方法
64 Constructor c=clazz.getConstructor(Integer.TYPE);
65
66 //获取各种级别的某个构造方法
67 c=clazz.getDeclaredConstructor(String.class);
68
69
70
71
72
73
74
75
76
77 //使用某个构造方法创建对象
78 c.setAccessible(true);
79 Object object=c.newInstance("小李");//相当于上转型对象
80 System.out.println(object);
81
82 //直接使用类创建对象,局限与无参
83 Object obj=clazz.newInstance();
84 System.out.println(obj);
85 //获取对应构造方法的参数对应的类数组
86 Class [] types=c.getParameterTypes();
87 for (Class type : types) {
88 //System.out.println(type);
89 }
90
91 //获取构造方法所在的类
92 Object class1=c.getDeclaringClass();
93 //System.out.println(class1);
94
95 //获取构造方法的访问权限
96 int result=c.getModifiers();
97 String authority=Modifier.toString(result);
98 //System.out.println(authority);
99
100 //获取构造方法的名字
101 String constructorName=c.getName();
102 //System.out.println(constructorName);
103
104
105
106
107
108
109
110
111
112
113
114 //获取public级别的方法 并且会获取到继承的public的方法
115 Method [] methhods=clazz.getMethods();
116 for (Method method : methhods) {
117 //System.out.println(method.getName());
118 }
119
120 //获取本类中的所有的方法
121 Method [] methods1=clazz.getDeclaredMethods();
122 for (Method method : methods1) {
123 //System.out.println(method.getName());
124 }
125
126
127 //调用类中的方法
128 //获取某个方法
129 Method m=clazz.getDeclaredMethod("getName", String.class,Integer.TYPE,int [].class);
130 //以下代表为调用了这个方法,并且传入了参数
131 m.setAccessible(true);//一般这个方法是私有的才会设置
132 //m.invoke(clazz.getConstructor(Integer.TYPE).newInstance(15), "liyinxia",15,new int[] {0,1,2,3});
133
134 //获取方法所在的类
135 Class class2 = m.getDeclaringClass();
136 //System.out.println(class2);
137
138 //获取方法的访问权限
139 /*int res=m.getModifiers();
140 String authorityName=Modifier.toString(res);
141 System.out.println(authorityName);*/
142
143 //获取方法的名字
144 /*String name3=m.getName();
145 System.out.println(name3);*/
146
147 //获取方法的参数
148 /*Object [] parameterTypes=m.getParameterTypes();
149 for (Object parameterType : parameterTypes) {
150 System.out.println(parameterType);
151 }*/
152
153
154 //获取方法的返回值类型
155 Class returnType=m.getReturnType();
156 //System.out.println(returnType);
157
158 //返回由Method对象表示方法抛出异常类型对应Class对象组成的数组
159 Class [] exceptionTypes = m.getExceptionTypes();
160 System.out.println("异常的个数"+exceptionTypes.length);
161 /*for (Class exceptionType : exceptionTypes) {
162 System.out.println(exceptionType);
163 }*/
164
165
166
167
168 //获取属性
169 Field shuXing=clazz.getField("name");
170
171 Object instance=clazz.getDeclaredConstructor().newInstance();
172 //设置属性的值
173 shuXing.set(instance, "xiaowang");
174 //获取属性的值
175 String name4=(String) shuXing.get(instance);
176 System.out.println(name4);
177
178
179
180 } catch (NoSuchMethodException e) {
181 e.printStackTrace();
182 } catch (ClassNotFoundException e) {
183 // TODO Auto-generated catch block
184 e.printStackTrace();
185 } catch (InstantiationException e) {
186 // TODO Auto-generated catch block
187 e.printStackTrace();
188 } catch (IllegalAccessException e) {
189 // TODO Auto-generated catch block
190 e.printStackTrace();
191 } catch (IllegalArgumentException e) {
192 // TODO Auto-generated catch block
193 e.printStackTrace();
194 } catch (InvocationTargetException e) {
195 // TODO Auto-generated catch block
196 e.printStackTrace();
197 } catch (NoSuchFieldException e) {
198 // TODO Auto-generated catch block
199 e.printStackTrace();
200 } catch (SecurityException e) {
201 // TODO Auto-generated catch block
202 e.printStackTrace();
203 }
204
205 }
206 }
以上代码已经非常详细的讲述了反射所具有的使用方法。。。。。。。。。仔细研究,其实也很简单,主要要理解反射存在的意义。
反射:是一种新的操作类中的构造方法,成员变量,普通方法的机制。
package testJavaSE;
import java.lang.reflect.Method;
public class testMethodPerson {
public static void main(String[] args) throws Exception {
//获得Person的Class对象
Class<?> cls = testJavaSE.Person.class;//Class.forName("testJavaSE.Person");
//创建Person实例
Person p = (Person)cls.newInstance();
//获得Person的Method对象,参数为方法名,参数列表的类型Class对象
Method method = cls.getMethod("eat",String.class);
//invoke方法,参数为Person实例对象,和想要调用的方法参数
String value = (String)method.invoke(p,"肉");
//输出invoke方法的返回值
System.out.println(value);
//控制台输出:
//吃肉
//返回值
}
}
class Person{
public String eat(String food) {
System.out.println("吃"+food);
return "返回值";
}
}
eclipse中如何去除警告:Class is a raw type. References to generic type Class<T> should be parameterized
转自: 锐洋智能
解决前的源代码:
public class test {
public static void main(String args[]) {//入口
try {
//假设在同一个包中建的一个javaBean: person
Class c = Class.forName("person");//警告出现在这里
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
System.out.println(factory.getName());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------
- 解决方法1:增加编译注解@SuppressWarnings("unchecked")
public class test {
@SuppressWarnings("unchecked")//增加在这里
public static void main(String args[]) {
try {
Class c = Class.forName("person");//警告出现在这里
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
...以下省略
------------------------------
- 解决方法2:使用泛型通配符
public class test {
public static void main(String args[]) {//入口
try {
Class<?> c = Class.forName("person");//这里使用泛型通配符
try {
person factory = (person) c.newInstance();
factory.setName("asdf");
System.out.println(factory.getName());
...以下省略


浙公网安备 33010602011771号