package 反射;
public class A {
private String a ="张三" ;
private int m =5;
public A(){}
public A(String a){
this.a = a;
}
public void m( ){
System.out.println(a);
}
}
package 反射;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class test4 {
public static void main(String[] args) {
A a1 = new A("王二麻子");
Class c = a1.getClass();
try {
Field f =c.getDeclaredField("a"); //A 中的常量名称
f.setAccessible(true);
String Value = f.get(a1)+" "; //a1是new出来的对象名 通过常量名称得到常量值
Constructor cons = c.getConstructor(String.class); //创建调用有参获取构造方法
A obj = (A)cons.newInstance(Value); //调用
obj.m();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
结果:
王二麻子
package 反射;
import java.lang.reflect.*;
public class Test3 {
public static void main(String[]args){
/*System.out.println(Direction.EAST);
Direction.WEST.show();
//判断两个枚举是否相同
Direction dir1 = Direction.EAST;
System.out.println(dir1 == Direction.EAST);//判断枚举是否相同的方法
System.out.println(dir1.compareTo(Direction.WEST));//枚举下标相减 如果为0则代表两个枚举相同
System.out.println(dir1.ordinal());//枚举下标
System.out.println(dir1.name());//枚举字符串*/
Class c = A.class;//类
/* try {
Field f = c.getField("a");
System.out.println(f.getName());
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
*/
//通过类获取里面的成员变量
Field [] fields = c.getFields();
for(Field f : fields){
System.out.println(f.getName());
}
//获取所有方法
//Method [] ms = c.getMethods();
//获取当前类的方法
/*Method [] ms = c.getDeclaredMethods();
for(Method m : ms){
System.out.println(m.getName());
}*/
//反射创建对象
/*Object obj = null;
try {
// obj = c.newInstance();//调用无参构造创建对象
//获取构造方法
Constructor cons = c.getConstructor(String.class);
obj = cons.newInstance("王五");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
Field field = c.getDeclaredField("a");
field.setAccessible(true);
field.set(obj, "钱三");//设置成员变量的值
System.out.println(field.getName());
System.out.println(field.get(obj));//获取成员变量的值
System.out.println(Modifier.toString(field.getModifiers()));//获取修饰符
System.out.println(field.getType());//获取类型
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
try {
Method m = c.getMethod("a",String.class,int.class);
//执行方法
m.invoke(obj ,"a",1);
System.out.println(Modifier.toString(m.getModifiers()));//获取修饰符
System.out.println(m.getReturnType());//获取返回类型
Parameter [] ps = m.getParameters();
for(Parameter p : ps){
System.out.println(p.getName());//参数名
System.out.println(p.getType());//参数类型
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}*/
try {
Class msg = Class.forName("message.Message");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}