使用反射获取属性、方法和赋值
public class clazz {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("clazz.Dog");
Object o3 = clazz.getDeclaredConstructor().newInstance();
Field name1 = clazz.getDeclaredField("name");
Field age1 = clazz.getDeclaredField("age");
name1.setAccessible(true);
age1.setAccessible(true);
name1.set(o3, "苹果");
age1.set(o3, 12);
Method show1 = clazz.getMethod("show" );
Method home = clazz.getDeclaredMethod("home", int.class, double.class);
show1.invoke(o3);
home.setAccessible(true);
Object invoke = home.invoke(o3, 10, 20);
System.out.println("invoke = " + invoke);
}
}