- 1. getDeclaredMethods() - 获取所有方法
- 2. getMethods() - 获取所有public修饰的方法
- 3. getMethod(String name, Class>… parameterTypes) - 获取指定构造方法
- 4. getDeclaredMethod(String name, Class>… parameterTypes) - 获取指定public修饰的方法
- 5. 调用方法
当获得某个类对应的Class对象后,就可以通过该Class对象的getMethods()方法或者getMethod()方法来获取全部方法或指定方法——这两个方法的返回值是Method数组,或者Method对象。
public class Person {
//私有的成员变量
private String name;
//默认的
int age;
//公共的
public String address;
public Person() {
super();
}
//没有用public修饰的构造方法
Person(int age, String address) {
super();
this.age = age;
this.address = address;
}
//私有的构造方法
private Person(String name) {
super();
this.name = name;
this.age = age;
}
//公共的构造方法
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public void show() {
System.out.println("show");
}
public void method(String s) {
System.out.println("method"+s);
}
public String getString(String s,int i) {
return s+"....."+i;
}
public void function() {
System.out.println("function");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
1. getDeclaredMethods() - 获取所有方法
getDeclaredMethods() :返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException {
Class> clazz = Class.forName("com.heng.Person");
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
}
}
2. getMethods() - 获取所有public修饰的方法
getMethods():获取Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共成员方法。
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException {
Class> clazz = Class.forName("com.heng.Person");
Method[] methods = clazz.getMethods();
for (Method method : methods) {
System.out.println(method);
}
}
}
3. getMethod(String name, Class>… parameterTypes) - 获取指定构造方法
getMethod(String name, Class>… parameterTypes) :class 对象所表示的类或接口的指定公共成员方法。
- name:方法名称
- parameterTypes:方法参数类型
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException {
Class> clazz = Class.forName("com.heng.Person");
// 获取show()方法,没有参数
Method show = clazz.getMethod("show");
System.out.println(show);
}
}
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException {
Class> clazz = Class.forName("com.heng.Person");
// 获取method()方法,有一个String类型的参数
Method method = clazz.getMethod("method",String.class);
System.out.println(method);
}
}
4. getDeclaredMethod(String name, Class>… parameterTypes) - 获取指定public修饰的方法
getDeclaredMethod(String name, Class>… parameterTypes) :
返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException {
Class> clazz = Class.forName("com.heng.Person");
// 获取method()方法,有一个String类型的参数
Method method = clazz.getDeclaredMethod("method",String.class);
System.out.println(method);
}
}
5. 调用方法
每个Method对象对应一个方法,获得Method对象后,程序就可通过该Method来调用它对应的方法。在Method里包含一个invoke()方法,该方法的签名如下:
Object invoke(Object obj,Object…args):该方法中的obj是执行该方法的主调,后面的args是执行该方法时传入该方法的实参。
class Solution {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class> clazz = Class.forName("com.heng.Person");
// 获取对象
Object object = clazz.newInstance();
// 获取method()方法,有一个String类型的参数
Method method = clazz.getDeclaredMethod("method",String.class);
// 执行方法,调用object对象的method方法,这个object对象就是person对象
method.invoke(object,"哈哈哈。。。。");
}
}



