java反射

package limengtong.com;

/**
 * @author 
 * @date 2020/7/2 - 19:15
 */
public class user {
    public int age;
    private String name;
    String add;

    public user(){
        System.out.println("空的构造方法被调用");
    }

    public user(int age, String name, String add) {
        this.age = age;
        this.name = name;
        this.add = add;
        System.out.println("public 有参数构造函数被调用");
    }

    private  user(int age){
        System.out.println("私有 有参数构造方法被调用");
        this.age = age;
        name = "LMT2";
        add  = "山东省";
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getAdd() {
        return add;
    }

    public void setAge(int age) {
        this.age = age;
        System.out.println("公共方法被调用");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAdd(String add) {
        this.add = add;
    }

    public void printInfo(){
        System.out.println("name: "+name);
        System.out.println("age: "+age);
        System.out.println("add: "+add);
    }

    private void printlnName(){
        System.out.println("name: "+name);
    }
}

 


package limengtong.com;
import java.lang.reflect.*;

/**
 * @author 
 * @date 2020/7/2 - 19:08
 */
public class test_fanshe {



    public void test_forName(){
        String name = "java.lang.String";
        Class c = null;
        try {
            c = Class.forName(name);
            System.out.println(c.getName());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void test_getLCass(){
        String name = "LMT";
        Class c = name.getClass();
        System.out.println(c.getName());
    }


    public  void test_Modifier(){
        user a = new user();
        System.out.println(Modifier.toString(a.getClass().getModifiers()));
    }

    //获取类的所有构造方法
    public void test_getConstructor0(){
        user a = new user();
        Class c = a.getClass();
        Constructor[] constructors;
        constructors = c.getDeclaredConstructors();
        for(int i=0;i<constructors.length;i++) {
            System.out.print(Modifier.toString(constructors[i].getModifiers()) + "参数:");
            Class[] parametertypes = constructors[i].getParameterTypes();
            for (int j = 0; j < parametertypes.length; j++) {
                System.out.print(parametertypes[j].getName() + " ");
            }
            System.out.println(" ");
        }
    }


    //获取类的特定构造方法
    public void test_getConstructor1(){
        user a = new user();
        Class c = a.getClass();
        try {
            //获取空的构造方法
            Constructor constructor = c.getDeclaredConstructor();
            System.out.println(Modifier.toString(constructor.getModifiers())+" 参数个数:"+constructor.getParameterTypes().length);


            //获取指定构造方法
            Class[] p = {int.class,String.class,String.class};

            Constructor constructor1 = c.getDeclaredConstructor(p);
            System.out.println(Modifier.toString(constructor1.getModifiers())+" 参数个数:"+constructor1.getParameterTypes().length);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

    //调用构造方法
    public void test_contructor2(){
        Class[] p = {int.class,String.class,String.class};


        Class c =user.class;
        Constructor declaredConstructor;
        try {
            declaredConstructor = c.getDeclaredConstructor(p);
            declaredConstructor.newInstance(24, "LMT", "烟台大学");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }



        //调用私有构造方法
        Class[] p2 = {int.class};
        Class c2 = user.class;
        try {
            Constructor declaredConstructor1 = c2.getDeclaredConstructor(p2);
            declaredConstructor1.setAccessible(true);//设置为可获得
            declaredConstructor1.newInstance(15);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //调用方法
    public void test_Method(){
        Class[] p  = {int.class,String.class,String.class};
        Class c = user.class;

        try {
            Constructor constructor = c.getDeclaredConstructor(p);
            Object o = constructor.newInstance(15, "LMT", "烟台大学");
            Class[] p2 = {int.class};
            Method method = c.getDeclaredMethod("printlnName");
            method.setAccessible(true);
            method.invoke(o);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    //获取参数
    public void test_field(){
        Class c = user.class;

        try {
            Constructor constructor = c.getDeclaredConstructor();
            Object o = constructor.newInstance();
            Field field = c.getDeclaredField("name");
            field.setAccessible(true);
            field.set(o,"LMT2");

            Method method = c.getDeclaredMethod("getName");
            Object s = method.invoke(o);
            String name = (String)s;
            System.out.println(name);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        test_fanshe test = new test_fanshe();
//        test.test_forName();
//        test.test_Modifier();
//        test.test_getConstructor0();
//        test.test_getConstructor1();
//        test.test_contructor2();
//        test.test_Method();
        test.test_field();
    }



}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值