青岛网站,湖南人文科技学院全国排名,福建网站建设,中山企业推广网站制作Method Class.getMethod(String name, Class... parameterTypes)的作用是获得对象所声明的公开方法该方法的第一个参数name是要获得方法的名字#xff0c;第二个参数parameterTypes是按声明顺序标识该方法形参类型。person.getClass().getMethod(Speak, null)…Method Class.getMethod(String name, Class... parameterTypes)的作用是获得对象所声明的公开方法该方法的第一个参数name是要获得方法的名字第二个参数parameterTypes是按声明顺序标识该方法形参类型。person.getClass().getMethod(Speak, null);//获得person对象的Speak方法因为Speak方法没有形参所以parameterTypes为nullperson.getClass().getMethod(run, String.class);//获得person对象的run方法因为run方法的形参是String类型的所以parameterTypes为String.class如果对象内的方法的形参是int类型的则parameterTypes是int.class本人写了一个例子来帮助大家来理解此方法的作用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);}}本文转自 https://blog.csdn.net/Handsome_fan/article/details/54846959