Java Class.getMethod()/getDeclaredMethods返回Method对象

Class.getMethod():获取当前类及所有继承的父类的public修饰的方法。仅包括public

Class.getDeclaredMethod():获取当前类的所有方法,包括public/private/protected/default修饰的方法。

定义

Method getMethod(String name, Class<?>... parameterTypes)  

该方法的第一个参数name是要获得方法的名字,第二个参数parameterTypes是按声明顺序标识该方法形参类型。

返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定公共成员方法。方法后面接收的就是Class类的对象,而如:String.class、int.class这些字节码才是Class类的对象。

如一个函数 int Test(int a, String str)对应的getMethod方法:

1. getMethod("Test",int.class,String.class);

2. getMethod("Test",new Class[]{int.class,String.class});

例子

实例一

package com.redsun;
public class TestBase {
    public String str = "hello,china!";
    protected String say() {
        return "hello,seo";
    }

   protected void say(String str) {
      this.str = str;
   }
}
package com.redsun;
import java.lang.reflect. * ;
public class TestDemo {
    public static void main(String[] args) {

        TestDemo cls = new TestDemo();
        Class c = cls.getClass();

        try {
            // parameter type is null
            Method m = c.getDeclaredMethod("say", null);
            System.out.println("method = " + m.toString());

            // method String
            Class[] cArg = new Class[1];
            cArg[0] = String.class;
            Method lMethod = c.getDeclaredMethod("say", cArg);
            System.out.println("method = " + lMethod.toString());
        } catch(NoSuchMethodException e) {
            System.out.println(e.toString());
        }
    }
}

实例二

//Person类:
package fyh.reflectDemo;
public class Person {
    private String name;
    private int ID;
    public String speed;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public Person(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    public void Speak() {
        System.out.println("Hello! " + "My name is " + name);
    }
    public void run(String speed) {
        System.out.println("I can run " + speed + " KM!!!");
    }
}
//testMain类:
package fyh.reflectDemo;
import java.lang.reflect.Method;
public class testMain {
    public static void main(String[] args) throws Exception {
        Person person = new Person("小明", 10001);
        person.Speak();
        person.run("10");
        Method m1 = person.getClass().getMethod("Speak", null);
        Method m2 = person.getClass().getMethod("run", String.class);
        System.out.println(m1);
        System.out.println(m2);
    }
}

运行结果:

Java Class.getMethod()例子

实例三

package com.westward;
public class Car {
    private void bell(String time){
        System.out.println("didi---"+time);
    }
    
    public void run(){
        System.out.println("running");
    }
}
package com.westward;
import java.lang.reflect.Method;
public class CarTest {
    public static void main(String[] args) {
        Class < ?>car = Car.class;
        Method[] methods1 = car.getMethods();
        Method[] methods2 = car.getDeclaredMethods();
        System.out.println("\n------------method.toString--------------");
        for (Method method1: methods1) {
            System.out.println(method1);
        }
        System.out.println("\n");
        for (Method method2: methods2) {
            System.out.println(method2);
        }
        System.out.println("\n------------method.getName--------------");
        for (Method method1: methods1) {
            System.out.println(method1.getName());
        }
        System.out.println("\n");
        for (Method method2: methods2) {
            System.out.println(method2.getName());
        }
    }
}

执行结果:

------------method.toString--------------
public void com.westward.Car.run()
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) 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()


public void com.westward.Car.run()
private void com.westward.Car.bell(java.lang.String)

------------method.getName--------------
run
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。