[Java] 纯文本查看 复制代码
Class clazz = Class.forName("com.heima.bean.Person");
Constructor c = class.getConsturctor(String.class, int.class);
Person p = (Person)c.newInstance("张三",20);
[Java] 纯文本查看 复制代码
Class clazz = Class.forName("com.heima.bean.Person");
Constructor c = class.getConsturctor(String.class, int.class);
Person p = (Person)c.newInstance("张三",20);
//Field f = clazz.getField("name"); //name私有了
//f.set(p, "李四");
Field f = clazz.getDeclaredField("name"); //暴力反射
f.setAccessible(true);
f.set(p, "李四");
[Java] 纯文本查看 复制代码
Class.getMethod("eat")
invoke(obj) (调用)
Class.getMethod("eat",int.class)
invoke(obj,10)
Method m = clazz.getMethod("eat")