反射:
public class TestPerson {
public static void main(String[] args) throws Exception {
Person zhangsan=new Person("张三",24);
reflectOpe4();
}
public static void getClazz() throws Exception {
Person zhangsan=new Person();
Class<?> class1=zhangsan.getClass();
System.out.println(class1.hashCode());
Class<?> class2=Person.class;
System.out.println(class2.hashCode());
Class<?> class3=Class.forName("com.qf.chap17_1.Person");
System.out.println(class3.hashCode());
}
public static void reflectOpe1() throws Exception {
Class<?> class1=Class.forName("com.qf.chap17_1.Person");
System.out.println(class1.getName());
System.out.println(class1.getPackage().getName());
System.out.println(class1.getSuperclass().getName());
Class<?>[] classes=class1.getInterfaces();
System.out.println(Arrays.toString(classes));
System.out.println(class1.getSimpleName());
System.out.println(class1.getTypeName());
}
public static void reflectOpe2() throws Exception{
Class<?> class1=Class.forName("com.qf.chap17_1.Person");
Constructor<?>[] cons=class1.getConstructors();
for (Constructor<?> con : cons) {
System.out.println(con.toString());
}
Constructor<?> con=class1.getConstructor();
Person zhangsan=(Person)con.newInstance();
System.out.println(zhangsan.toString());
Person wangwu=(Person)class1.newInstance();
System.out.println(wangwu.toString());
Constructor<?> con2=class1.getConstructor(String.class,int.class);
Person xiaoli=(Person)con2.newInstance("晓丽",20);
System.out.println(xiaoli.toString());
}
public static void reflectOpe3() throws Exception{
Class<?> class1=Class.forName("com.qf.chap17_1.Person");
Method[] methods=class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.toString());
}
Method eatMethod=class1.getMethod("eat");
Person zhangsan=(Person)class1.newInstance();
zhangsan.setName("张三");
eatMethod.invoke(zhangsan);
System.out.println("------------------");
Method toStringMethod=class1.getMethod("toString");
Object result=toStringMethod.invoke(zhangsan);
System.out.println(result);
System.out.println("-------------------");
Method eatMethod2=class1.getMethod("eat", String.class);
eatMethod2.invoke(zhangsan, "鸡腿");
Method privateMethod=class1.getDeclaredMethod("privateMethod");
privateMethod.setAccessible(true);
privateMethod.invoke(zhangsan);
Method staticMethod=class1.getMethod("staticMethod");
staticMethod.invoke(null);
}
public static Object invokeAny(Object obj,String methodName,Class<?>[] types,Object...args) throws Exception {
Class<?> class1=obj.getClass();
Method method=class1.getMethod(methodName, types);
return method.invoke(obj, args);
}
public static void reflectOpe4() throws Exception{
Class<?> class1=Class.forName("com.qf.chap17_1.Person");
Field[] fields=class1.getDeclaredFields();
System.out.println(fields.length);
for (Field field : fields) {
System.out.println(field.toString());
}
Field namefield=class1.getDeclaredField("name");
namefield.setAccessible(true);
Person zhangsan=(Person)class1.newInstance();
namefield.set(zhangsan, "张三");
System.out.println(namefield.get(zhangsan));
}
}