获取方法
在进行反射处理的时候也可以通过反射来获取类之中的全部方法,但是需要提醒的是,如果要想通过反射调用这些方法,必须在类之中提供有实例化对象。
在Class类里面提供有如下的操作可以获取方法对象:
- 获取全部方法(反映此
类
对象表示的类或接口的所有已声明方法,但不包括继承的方法): public Method[] getDeclaredMethods() throws SecurityException - 获取指定方法:public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
- 获取公共全部方法(包括由类或接口声明的那些以及从超类和超接口继承的那些):public Method[] getMethods() throws SecurityException
- 获取公共指定方法:public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
范例:对以上的方法进行获取
Person类定义
package reflect;
public class Person {
public Person(){ //无参构造
System.out.println("Person类的无参构造方法");
}
public void show1(int num){
System.out.println("我是public的show1方法,我的值是"+num);
}
protected void show2(){
System.out.println("我是protected的show2方法");
}
private void show3(){
System.out.println("我是私有的show3方法");
}
public void testShow(){
System.out.println("我是从父接口继承过来的public类型的testShow方法");
}
}
测试类
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Demo{
public static void main(String[] args) throws Exception{
Class cls =Person.class; //获取对象
System.out.println("------获取全部方法(不包括继承)------");
Method[] method1= cls.getDeclaredMethods();
for(Method method : method1){
System.out.println(method);
}
System.out.println("------获取全部公共方法(包括继承)------");
Method[] method2 = cls.getMethods();
for (Method method:method2){
System.out.println(method);
}
}
}
------获取全部方法(不包括继承)------
protected void reflect.Person.show2()
public void reflect.Person.testShow()
private void reflect.Person.show3(int,java.lang.String)
public void reflect.Person.show1()
------获取全部公共方法(包括继承)------
public void reflect.Person.testShow()
public void reflect.Person.show1()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
其中,Method中有一个非常重要的方法:
在具有指定参数的指定对象上调用此方法
对象表示的基础方法:
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
范例:在不导入指定类开发包的情况下调用show1方法
package reflect;
import java.lang.reflect.Method;
public class Demo{
public static void main(String[] args) throws Exception{
Class<?> cls =Class.forName("reflect.Person"); //获取指定类的Class对象
int num = 1; //要操作的类属性
//1、任何情况下如果要想保存类中的属性或者调用类中的方法都必须保证有实例化对象
Object obj = cls.getDeclaredConstructor().newInstance(); //调用无参构造实例化对象
//2、要想进行方法的调用一定要先获取方法的名称
String setMethodName = "show1";
Method method = cls.getDeclaredMethod(setMethodName,int.class);
method.invoke(obj,num); //等价于Person对象.show1(num);
}
}
Person类的无参构造方法
我是public的show1方法,我的值是1
利用此类操作整体的形式上不会有任何明确的类对象产生,一切都是依靠反射机制处理的,这样的处理避免了与某一个类的耦合问题。