码迷,mamicode.com
首页 > 其他好文 > 详细

反射——获取成员方法

时间:2019-08-21 21:21:50      阅读:104      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!