第一次提交
This commit is contained in:
78
Java/code/反射代码/RefEx.java
Normal file
78
Java/code/反射代码/RefEx.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package test;
|
||||
|
||||
import static java.lang.System.out;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RefEx {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
String s="hello";
|
||||
Class c=s.getClass();//获取类型
|
||||
System.out.println(c);
|
||||
String modifiers=Modifier.toString(c.getModifiers());//获取修饰符
|
||||
System.out.println(modifiers);
|
||||
|
||||
TypeVariable<?>[] tv = c.getTypeParameters();//获取泛型类型参数
|
||||
if (tv.length != 0) {
|
||||
out.format(" ");
|
||||
for (TypeVariable<?> t : tv)
|
||||
out.format("%s ", t.getName());
|
||||
out.format("%n%n");
|
||||
} else {
|
||||
out.format(" -- No Type Parameters --%n%n");
|
||||
}
|
||||
|
||||
Class superclass=c.getSuperclass();//获取类型的父类
|
||||
System.out.println(superclass);
|
||||
|
||||
Type[] intfs = c.getGenericInterfaces();//获取类实现的接口
|
||||
if (intfs.length != 0) {
|
||||
for (Type intf : intfs)
|
||||
out.format(" %s%n", intf.toString());
|
||||
out.format("%n");
|
||||
} else {
|
||||
out.format(" -- No Implemented Interfaces --%n%n");
|
||||
}
|
||||
|
||||
Annotation[] ann = c.getAnnotations();//获取类的注解
|
||||
if (ann.length != 0) {
|
||||
for (Annotation a : ann)
|
||||
out.format(" %s%n", a.toString());
|
||||
out.format("%n");
|
||||
} else {
|
||||
out.format(" -- No Annotations --%n%n");
|
||||
}
|
||||
|
||||
Constructor[] cons=c.getDeclaredConstructors();//获取类的构造方法
|
||||
for(Constructor con:cons){
|
||||
System.out.println(con.toGenericString());
|
||||
|
||||
}
|
||||
out.format("%n%n");
|
||||
|
||||
Field[] fs=c.getDeclaredFields();//获取类的数据成员(成员变量)
|
||||
for(Field f:fs){
|
||||
System.out.println(f.toGenericString());
|
||||
|
||||
}
|
||||
out.format("%n%n");
|
||||
|
||||
Method[] ms=c.getMethods();//获取类的公有成员方法(包含从父类继承的)
|
||||
for(Method m:ms){
|
||||
System.out.println(m.toGenericString());
|
||||
|
||||
}
|
||||
out.format("%n%n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
87
Java/code/反射代码/Test.java
Normal file
87
Java/code/反射代码/Test.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package test;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Test {
|
||||
public static String classname="类.Student";
|
||||
public static String fieldname="name";
|
||||
public static String methodname="setName";
|
||||
public static String[] mptypes= {"java.lang.String"};
|
||||
public static Object fvalue=new String("xiaolin");
|
||||
public static Object mp1=new String("xiaohong");
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
try {
|
||||
Class c=Class.forName(classname);//根据类名字符串获得类对应的Class实例
|
||||
//第一种用反射创建类对象的方法,相当于调用类的无参数构造方法
|
||||
Object o=c.newInstance();//Student o=new Student();
|
||||
|
||||
//使用反射访问类对象的属性(给属性设置值,获取属性的值)
|
||||
Field f=c.getDeclaredField(fieldname);//根据Field名,获得Field对象
|
||||
f.setAccessible(true);
|
||||
f.set(o,fvalue);//o.name="xiaolin";使用反射给o对象的f代表的属性设置值为发value的值
|
||||
System.out.println(f.get(o));//获取o对象的f属性对应的值,并输出
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
//使用反射调用对象的方法
|
||||
//使用反射根据方法名,和参数类型、参数数量来获取具体某个方法
|
||||
Method m=c.getDeclaredMethod(methodname, Class.forName(mptypes[0]));//String.class
|
||||
//方法的调用,调用o对象的m方法,方法的实际参数是mp1,mo是方法调用的返回值
|
||||
Object mo=m.invoke(o,mp1);//o.setName("xiaohong");
|
||||
System.out.println(mo);
|
||||
//调用了o对象名为setAge,参数有1个,类型是int型的方法,实际参数是21
|
||||
m=c.getDeclaredMethod("setAge", int.class);
|
||||
m.invoke(o, 21);
|
||||
System.out.println(o);
|
||||
|
||||
/*Method m=c.getDeclaredMethod(methodname);
|
||||
Object mo=m.invoke(o);
|
||||
System.out.println(mo);
|
||||
System.out.println(f.get(o));*/
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
//第二种用反射创建类对象的方法,先获取具体的Constructor,再用Constructor去创建对象
|
||||
Constructor con=c.getDeclaredConstructor(String.class,int.class);//根据构造方法参数类型来获取某个构造方法
|
||||
Object o1=con.newInstance("Mary",20);
|
||||
//用Constructor创建类对象,相当于Student o1=new Student("Mary",20);
|
||||
System.out.println(o1);
|
||||
|
||||
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchFieldException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user