Method对象中invoke方法中第一个参数表示____
的有关信息介绍如下:invoke方法的第一个参数是一个对象。
此对象可以为:①方法持有者;②方法持有者的继承者。
例子:父类有方法
public class Father {
public void say(String name){
System.out.println("我是father叫"+name);
}
}子类有方法
public class Son extends Father {
@Override
public void say(String name){
System.out.println("我是son我叫"+name);
}
}测试类
public class TestInvoke {
public void test() throws Exception {
Class clazz = Class.forName("CloneObject.Father");//反射父类
Father father = new Father();//实例化父类
Son son = new Son();//实例化子类
Object object=clazz.newInstance();//获得一个新的父类对象
Method method = clazz.getMethod("say",String.class);
method.invoke(father,"爸爸");
method.invoke(son,"孩子");
method.invoke(object,"新的父亲");
}
public static void main(String[] args) throws Exception {
new TestInvoke().test();
}
}结果
我是father叫爸爸
我是son我叫孩子
我是father叫新的父亲