java反射

package reflection;

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

public class ReflectionAPIDemo {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//        获取Employee这个类所关联的Class对象
        Class<?> classType = Class.forName("reflection.Employee2");
//        通过反射机制来构造Employee这个类的实例对象(默认调用无参构造)
        //Employee2 employee2 = (Employee2) classType.newInstance();

//        调用指定的方法来构造对象(得到无参构造方法)
//        Constructor<?> constructor = classType.getConstructor(new Class[]{});
//        Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{});
//        System.out.println(employee2);

        //        调用指定的方法来构造对象(得到有参构造方法)
        Constructor<?> constructor = classType.getConstructor(new Class[]{String.class,int.class});
        Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{"小王",30});
        System.out.println(employee2);

        //获得Class对象指定的方法,包括私有的方法
        Method method = classType.getDeclaredMethod("toString",new Class[]{});
        System.out.println(method.getName());
        //方法的调用
        String desc = (String)method.invoke(employee2,new Object[]{});
        System.out.println(desc);

//        获取Class对象所有的方法
        Method[] methods = classType.getDeclaredMethods();
        System.out.println(methods.length);
        for (Method m : methods             ) {
//            打印方法名+方法的访问权限+方法的返回值类型
            System.out.println(m.getName()+"---"+m.getModifiers()+"---"+m.getReturnType());
        }

//        改变私有方法的访问权限
        Method method1 = classType.getDeclaredMethod("work",new Class[]{});
        method1.setAccessible(true); //true代表是可以访问的
        method1.invoke(employee2,new Object[]{});

//        获取Class对象指定的属性,包括私有的
        Field field = classType.getDeclaredField("name");
        field.setAccessible(true);
        field.set(employee2,"鲁班七号");
        System.out.println(field.get(employee2));
    }

}

class Employee2{
    private String name;
    private int age;

    public Employee2() {
    }

    public Employee2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "Employee2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private void work(){
        System.out.println("看看能不能访问私有的方法");
    }
}


package reflection;

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

public class ReflectionAPIDemo {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//        获取Employee这个类所关联的Class对象
        Class<?> classType = Class.forName("reflection.Employee2");
//        通过反射机制来构造Employee这个类的实例对象(默认调用无参构造)
        //Employee2 employee2 = (Employee2) classType.newInstance();

//        调用指定的方法来构造对象(得到无参构造方法)
//        Constructor<?> constructor = classType.getConstructor(new Class[]{});
//        Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{});
//        System.out.println(employee2);

        //        调用指定的方法来构造对象(得到有参构造方法)
        Constructor<?> constructor = classType.getConstructor(new Class[]{String.class,int.class});
        Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{"小王",30});
        System.out.println(employee2);

        //获得Class对象指定的方法,包括私有的方法
        Method method = classType.getDeclaredMethod("toString",new Class[]{});
        System.out.println(method.getName());
        //方法的调用
        String desc = (String)method.invoke(employee2,new Object[]{});
        System.out.println(desc);

//        获取Class对象所有的方法
        Method[] methods = classType.getDeclaredMethods();
        System.out.println(methods.length);
        for (Method m : methods             ) {
//            打印方法名+方法的访问权限+方法的返回值类型
            System.out.println(m.getName()+"---"+m.getModifiers()+"---"+m.getReturnType());
        }

//        改变私有方法的访问权限
        Method method1 = classType.getDeclaredMethod("work",new Class[]{});
        method1.setAccessible(true); //true代表是可以访问的
        method1.invoke(employee2,new Object[]{});

//        获取Class对象指定的属性,包括私有的
        Field field = classType.getDeclaredField("name");
        field.setAccessible(true);
        field.set(employee2,"鲁班七号");
        System.out.println(field.get(employee2));
    }

}

class Employee2{
    private String name;
    private int age;

    public Employee2() {
    }

    public Employee2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "Employee2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private void work(){
        System.out.println("看看能不能访问私有的方法");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值