反射——获取成员方法

约定不等于承诺〃 2023-08-17 16:08 194阅读 0赞

1392562-20190821204654745-1047986953.png

public Method getMethod(String name, Class<?>… parameterTypes) 获取Public修饰的一个方法

public Method getDeclaredMethod(String name, Class<?>… parameterTypes) 获取所有权限的一个方法

public Method[] getMethods() 本类与父类中所有public 修饰的方法所有方法

public Method[] getDeclaredMethods() 获取本类中所有的方法

  1. package pers.reflect.person;
  2. public class Person {
  3. private String name;
  4. private int age;
  5. public String hobby;
  6. public String height;
  7. protected String sex;
  8. String address;
  9. public Person(){
  10. }
  11. public Person(String name,int age){
  12. this.name=name;
  13. this.age=age;
  14. }
  15. protected void sports() {
  16. System.out.println("我爱运动");
  17. }
  18. private void sport() {
  19. System.out.println("我不爱运动");
  20. }
  21. public String toString() {
  22. return "Person [name=" + name + ", age=" + age + ", hobby=" + hobby
  23. + ", height=" + height + ", sex=" + sex + ", address=" + address
  24. + "]";
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public int getAge() {
  33. return age;
  34. }
  35. public void setAge(int age) {
  36. this.age = age;
  37. }
  38. public void study(String name){
  39. System.out.println(name+"爱学习");
  40. }
  41. }

(1)获取公共的空参的成员方法:

  1. package pers.reflect.method;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.rmi.StubNotFoundException;
  5. import pers.reflect.person.Person;
  6. public class reflectDemo {
  7. public static void main(String[] args) throws SecurityException,
  8. NoSuchMethodException, IllegalArgumentException,
  9. IllegalAccessException, InvocationTargetException {
  10. Class c = Person.class;
  11. Method study_method = c.getMethod("study");
  12. Person p = new Person();
  13. study_method.invoke(p);
  14. }
  15. }

(2)获取公共的带参数的成员方法:

  1. package pers.reflect.method;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import pers.reflect.person.Person;
  5. public class reflectDemo {
  6. public static void main(String[] args) throws SecurityException,
  7. NoSuchMethodException, IllegalArgumentException,
  8. IllegalAccessException, InvocationTargetException {
  9. Class c = Person.class;
  10. Method study_method1 = c.getMethod("study",String.class);
  11. Person p = new Person();
  12. study_method1.invoke(p,"Tom");
  13. }
  14. }

(3)获取所有的公共的方法:

  1. package pers.reflect.method;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import pers.reflect.person.Person;
  5. public class reflectDemo {
  6. public static void main(String[] args) throws SecurityException,
  7. NoSuchMethodException, IllegalArgumentException,
  8. IllegalAccessException, InvocationTargetException {
  9. Class c = Person.class;
  10. Method[] methods = c.getMethods();
  11. for (Method method:methods){
  12. System.out.println(method);
  13. }
  14. }
  15. }

(4)获取方法名字:

1392562-20190821205755626-1087514034.png

  1. package pers.reflect.method;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import pers.reflect.person.Person;
  5. public class reflectDemo {
  6. public static void main(String[] args) throws SecurityException,
  7. NoSuchMethodException, IllegalArgumentException,
  8. IllegalAccessException, InvocationTargetException {
  9. Class c = Person.class;
  10. Method[] methods = c.getMethods();
  11. for(Method method:methods){
  12. System.out.println(method.getName());
  13. }
  14. }
  15. }

(5)获取所有的方法(忽略权限修饰符):

  1. package pers.reflect.method;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import pers.reflect.person.Person;
  5. public class reflectDemo {
  6. public static void main(String[] args) throws SecurityException,
  7. NoSuchMethodException, IllegalArgumentException,
  8. IllegalAccessException, InvocationTargetException {
  9. Class c = Person.class;
  10. Method[] methods = c.getDeclaredMethods();
  11. for(Method method:methods){
  12. method.setAccessible(true);
  13. System.out.println(method);
  14. }
  15. }
  16. }

转载于:https://www.cnblogs.com/zhai1997/p/11391275.html

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读