标签:int name declare http eth on() invoke res print
public Method getMethod(String name, Class<?>... parameterTypes) 获取Public修饰的一个方法
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) 获取所有权限的一个方法
public Method[] getMethods() 本类与父类中所有public 修饰的方法所有方法
public Method[] getDeclaredMethods() 获取本类中所有的方法
package pers.reflect.person; public class Person { private String name; private int age; public String hobby; public String height; protected String sex; String address; public Person(){ } public Person(String name,int age){ this.name=name; this.age=age; } protected void sports() { System.out.println("我爱运动"); } private void sport() { System.out.println("我不爱运动"); } public String toString() { return "Person [name=" + name + ", age=" + age + ", hobby=" + hobby + ", height=" + height + ", sex=" + sex + ", address=" + address + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void study(String name){ System.out.println(name+"爱学习"); } }
(1)获取公共的空参的成员方法:
package pers.reflect.method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.StubNotFoundException; import pers.reflect.person.Person; public class reflectDemo { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class c = Person.class; Method study_method = c.getMethod("study"); Person p = new Person(); study_method.invoke(p); } }
(2)获取公共的带参数的成员方法:
package pers.reflect.method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import pers.reflect.person.Person; public class reflectDemo { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class c = Person.class; Method study_method1 = c.getMethod("study",String.class); Person p = new Person(); study_method1.invoke(p,"Tom"); } }
(3)获取所有的公共的方法:
package pers.reflect.method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import pers.reflect.person.Person; public class reflectDemo { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class c = Person.class; Method[] methods = c.getMethods(); for (Method method:methods){ System.out.println(method); } } }
(4)获取方法名字:
package pers.reflect.method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import pers.reflect.person.Person; public class reflectDemo { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class c = Person.class; Method[] methods = c.getMethods(); for(Method method:methods){ System.out.println(method.getName()); } } }
(5)获取所有的方法(忽略权限修饰符):
package pers.reflect.method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import pers.reflect.person.Person; public class reflectDemo { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class c = Person.class; Method[] methods = c.getDeclaredMethods(); for(Method method:methods){ method.setAccessible(true); System.out.println(method); } } }
标签:int name declare http eth on() invoke res print
原文地址:https://www.cnblogs.com/zhai1997/p/11391275.html