152.反射テスト---オブジェクトメソッドを取得して実行する
2040 ワード
ReflexDemo04.java
出力:
import java.lang.reflect.Method;
public class ReflexDemo04 {
public static void main(String[] args) throws Exception {
Class pC = Person.class;
Person ps = new Person();
//
Method eat1 = pC.getMethod("eat");
eat1.invoke(ps);
//
Method eat2 = pC.getMethod("eat", String.class);
eat2.invoke(ps," ");
System.out.println("===================================");
Method[] methods = pC.getMethods();
for (Method method : methods) {
System.out.println(method);
System.out.println(method.getName());
}
}
}
出力:
!
:
===================================
public java.lang.String cn.lwgk.Reflex.Person.toString()
toString
public java.lang.String cn.lwgk.Reflex.Person.getName()
getName
public void cn.lwgk.Reflex.Person.setName(java.lang.String)
setName
public int cn.lwgk.Reflex.Person.getBir()
getBir
public void cn.lwgk.Reflex.Person.setBir(int)
setBir
public void cn.lwgk.Reflex.Person.setSchool(java.lang.String)
setSchool
public java.lang.String cn.lwgk.Reflex.Person.getSchool()
getSchool
public void cn.lwgk.Reflex.Person.setAge(int)
setAge
public int cn.lwgk.Reflex.Person.getAge()
getAge
public void cn.lwgk.Reflex.Person.eat()
eat
public void cn.lwgk.Reflex.Person.eat(java.lang.String)
eat
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait
public final void java.lang.Object.wait() throws java.lang.InterruptedException
wait
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait
public boolean java.lang.Object.equals(java.lang.Object)
equals
public native int java.lang.Object.hashCode()
hashCode
public final native java.lang.Class java.lang.Object.getClass()
getClass
public final native void java.lang.Object.notify()
notify
public final native void java.lang.Object.notifyAll()
notifyAll