反射获取构造方法,普通方法 及其使用

反射获取构造方法,普通方法

反射获取构造方法

image-20210402223641860

反射继承关系


实例化方法替代

clazz.getDeclaredConstructor().newInstance()

获取所有构造方法

public Constructor<?>[] getDeclaredConstructors()throws SecurityException

获取指定构造方法

public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException

获取所有构造方法

public Constructor<?>[] getConstructors()throws SecurityException

获取指定构造方法

public Constructor<T> getConstructor(Class<?>... parameterTypes)throws NoSuchMethodException,SecurityExcepton

反射获取普通方法

获取全部方法

public Method[] getMethods()throws SecurityException

获取指定方法

public Method getMethod(String name,Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException

获取本类全部方法

public Method[] getDeclaredMethods()throws SecurityException

获取本类指定方法

public Method getDeclaredMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException
package demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class test {
    public static void main(String args[]) throws Exception {
        Class<?> cls = Class.forName("DemoService.DemoServiceImpl.Person");
        //获取所有构造方法
        {
            Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors();
            //获取全部构造方法
            System.out.println("获取全部构造方法getDeclaredConstructors()");
            for (Constructor constructor : declaredConstructors) {
                System.out.println(constructor);
            }
        }
        {
            System.out.println("获取单个构造方法与实例化对象");
            Constructor<?> person = cls.getDeclaredConstructor(String.class, int.class);
            System.out.println("person有参构造:" + person.newInstance("name", 1).toString());
            Constructor<?> declaredConstructor = cls.getDeclaredConstructor();
            System.out.println("person无参构造" + declaredConstructor.getName());
        }
        //获取全部构造
        {
            Constructor<?>[] constructors = cls.getConstructors();
            System.out.println("获取全部构造getConstructors()");
            for (Constructor constructor : constructors) {
                System.out.println(constructor);
            }
        }
        //获取指定构造
        {
            Constructor<?> constructor = cls.getConstructor();
            System.out.println("获取指定构造->无参构造  getConstructor()");
            System.out.println(constructor);
            Constructor<?> constructor1 = cls.getConstructor(String.class, int.class);
            System.out.println("获取指定构造->有参构造  getConstructor()");
            System.out.println(constructor1.newInstance("laowang", 20).toString());
        }
        //------------------------- 获取普通方法 ------------------
        //获取所有普通方法(包括父类)
        {
            Method[] methods = cls.getMethods();
            System.out.println("获取全部普通方法");
            for (Method met : methods) {
                System.out.println(met);
            }
        }
        //获取指定方法包括父类
        {
            Method method = cls.getMethod("getName");
            System.out.println("获取单个普通方法(父类)");
            System.out.println(method);
        }
        //获取本类全部普通方法
        {
            Method[] declaredMethods = cls.getDeclaredMethods();
            System.out.println("获取本类所有普通方法");
            for (Method method : declaredMethods) {
                System.out.println(method);
            }
        }
        {
            Method toString = cls.getDeclaredMethod("channe");
            System.out.println("获取本类单个普通方法");
            System.out.println(toString);
        }
        //通过反射机制不会有任何明确的类对象产生,这样的处理避免了与某一个类的耦合问题
        {
            System.out.println("通过invoke 方法->使用Person中set get方法");
            String value = "xiaowang";
            //设置get set 的方法名
            Object o = cls.getDeclaredConstructor().newInstance();
            //实例化对象
            String setterName = "setName";
            //方法名称
            Method setMethod = cls.getDeclaredMethod(setterName, String.class);
            //获得制定的方法
            setMethod.invoke(o, value);
            //等价于 Person对象.setName(value)
            String getMethone = "getName";
            Method getMethod = cls.getDeclaredMethod(getMethone);
            //get方法
            System.out.println(getMethod.invoke(o));
            //等价于 Person对象.getName()
        }
    }
}
------------------------结果-----------------
获取全部构造方法getDeclaredConstructors()
public DemoService.DemoServiceImpl.Person()
public DemoService.DemoServiceImpl.Person(java.lang.String,int)
获取单个构造方法与实例化对象
person有参构造:姓名:name、年龄:1
person无参构造DemoService.DemoServiceImpl.Person
获取全部构造getConstructors()
public DemoService.DemoServiceImpl.Person()
public DemoService.DemoServiceImpl.Person(java.lang.String,int)
获取指定构造->无参构造  getConstructor()
public DemoService.DemoServiceImpl.Person()
获取指定构造->有参构造  getConstructor()
姓名:laowang、年龄:20
获取全部普通方法
public java.lang.String DemoService.DemoServiceImpl.Person.toString()
public java.lang.String DemoService.DemoServiceImpl.Person.getName()
public void DemoService.DemoServiceImpl.Person.setName(java.lang.String)
public void DemoService.DemoServiceImpl.Person.send()
public boolean DemoService.DemoServiceImpl.Person.channe()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
获取单个普通方法(父类)
public java.lang.String DemoService.DemoServiceImpl.Person.getName()
获取本类所有普通方法
public java.lang.String DemoService.DemoServiceImpl.Person.toString()
public java.lang.String DemoService.DemoServiceImpl.Person.getName()
public void DemoService.DemoServiceImpl.Person.setName(java.lang.String)
public void DemoService.DemoServiceImpl.Person.send()
public boolean DemoService.DemoServiceImpl.Person.channe()
获取本类单个普通方法
public boolean DemoService.DemoServiceImpl.Person.channe()
通过invoke 方法->使用Person中set get方法
xiaowang

Person类

package DemoService.DemoServiceImpl;

import DemoService.IchanneService;
import DemoService.ImessageService;
import demo.AbsPreson;

public class Person extends AbsPreson implements IchanneService, ImessageService {
    private String name;
    private int age;
    public Person(){}
    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public String getName () {
        return name;
    }
    public void setName (String name) {
        this.name = name;
    }
    public String toString() {
        return "姓名:"+this.name+"、年龄:"+this.age;
    }
    @Override
    public boolean channe() {
        return true;
    }
    @Override
    public void send() {
        if (this.channe()) {
            System.out.println("okbase");
        }
    }
}

接口

package DemoService;

public interface IchanneService {
    boolean channe();
}

package DemoService;

public interface ImessageService {
    void send();
}

Person 父类

package demo;

public abstract class AbsPreson {
    public AbsPreson() {}
    public AbsPreson(String name) {
    }
    public String getName() {
        return "你好";
    }
}
原文地址:https://www.cnblogs.com/laowt/p/14613808.html