比如一个person类 我想用反射的方法得到show方法,然后在使用
public class Test6 {
public static void main(String[] args) throws Exception{
Person person = new Person();
System.out.println(person.x);
person.show();
Person person1 = new Person(5,"haha");
Method personMethod = Class.forName("Person1").getMethod("show",null);
//这里Person1总是报错,该怎么解决,应该放什么进去
//这里getMethod后面是这样这的吗??
System.out.println(personMethod.invoke(person1));
}
class Person {
int x;
String name;
public Person() {
super();
x=1;
}
public Person(int x,String name) {
super();
this.x = x;
this.name = name;
}
public void show(){
System.out.println("x="+x+"---"+"name="+name);
}
} |
|