java反射

1.什么是反射?

反射是框架设计的灵魂,是间接操作对象机制,在获取Class类之后反向获取和操作对象

就是在运行时,把类中成员变量抽取为其他类对象过去

2.如何获取反射类对象。

有三种方式

1.通过Class.forName方式获取

Class<?> aClass = Class.forName("com.reflect.demo1.Demo1");

 2.通过 类名.class  方式获取

Class<Demo1> demo1Class = Demo1.class;

3.通过getClass() 方式获取

Demo1 demo1 = new Demo1();
 Class<? extends Demo1> aClass1 = demo1.getClass();

3.如何根据反射类获取对象。

通过Class类对象,调用newInstance()方法

Class<Demo1> demo1Class = Demo1.class;
        Demo1 demo11 = demo1Class.newInstance();
        System.out.println(demo11);

4.如何获取类中属性对象。

1.getDeclaredFields()

获取本类所有属性

 //获取本类所有的属性
        Field[] declaredFields = studentClass.getDeclaredFields();
        for(Field field:declaredFields){
            System.out.println(field);
        }

2.getDeclaredField()

获取本类单个属性,需要指定本类的属性名

 //获取本类指定 属性
        Field declaredField = studentClass.getDeclaredField("name");
        System.out.println(declaredField);

3.getFields()

获取本类及父类 public属性

//获取本类及父类 public属性
        Field[] fields = studentClass.getFields();
        for (Field field:fields){
            System.out.println("本类及父类public属性"+field);
        }

4.getField()

  获取本类及父类 单个public属性,需要指定本类及父类public的属性名

 //获取本类及父类 public 单个属性
        Field key = studentClass.getField("key");
        System.out.println(key);

4.2Field类常用方法

@Data

public class Student  extends Teacher{
   @you(value = "小样")
    private String name;
    private  Integer age;

    public String value;
}
@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)

//运行时生效
public @interface you {
    String value();
}
 public static void main(String[] args) throws Exception {
        //通过反射获取类对象
        Class<Student> studentClass = Student.class;
        //通过反射类 得到类对象
        Student student = studentClass.newInstance();
        System.out.println(student);
        //获取本类属性
        Field declaredField = studentClass.getDeclaredField("name");
                //允许访问私有属性
            declaredField.setAccessible(true);
            declaredField.set(student,"小样");

        System.out.println(student);

        you annotation = declaredField.getAnnotation(you.class);
        System.out.println(annotation.value());
    }

 访问prviate需要用到setAccessible(true)允许访问

5.如何获取类中方法对象。

1.getDeclaredMethods()

 //获取本类的 全部方法对象

        Method[] declaredMethods = smallClass.getDeclaredMethods();
        for (Method method:declaredMethods){
            System.out.println(method);
        }

2.getDeclaredMethod()

需要指定本类方法名

//获取本类 指定方法对象
        Method fun3 = smallClass.getDeclaredMethod("fun3");
        System.out.println(fun3);

3.getMethods()

Object是其所有方法的父类,因此object的public方法也会获取到
 //获取本类及父类的public 方法对象
        Method[] methods = smallClass.getMethods();
         for (Method method : methods) {
             System.out.println("本类及父类public方法对象"+method);
            
        }

4.getMethod();

需要指定本类及父类public方法名,有参数也要加上

//获取本类及父类指定 public 方法对象
        Method fun2 = smallClass.getMethod("fun2", int.class);
        System.out.println(fun2);

5.2Method类常用方法

public class Small {
    public String fun1(){
        System.out.println("----------1");
        return "1";
    }
    public String fun2(int age){
        System.out.println("----------2");
        return "2"+age;
    }
    private void fun3(){
        System.out.println("----------3");

    }
    @your("今天星期五")
    public String fun4(int age,String name){
        return age+name;
    }
}
//在方法上使用
@Target(value = ElementType.METHOD)
//运行时有效
@Retention(value = RetentionPolicy.RUNTIME)
public @interface your {
    String value();
}
public class Test4 {
    public static void main(String[] args) throws Exception {
        // 通过反射获取类对象
        Class<Small> smallClass = Small.class;
        // 得到类对象
        Small small = smallClass.newInstance();


        /*Metnod常用方法*/
        Method fun4 = smallClass.getMethod("fun4", int.class, String.class);
        Object f= fun4.invoke(small, 18, "小样");
        System.out.println(f);

        //使用注解
        your annotation =fun4.getAnnotation(your.class);
        System.out.println(annotation.value());

        Method f3 = smallClass.getDeclaredMethod("fun3");
        //允许访问private
        f3.setAccessible(true);
        Object invoke1 = f3.invoke(small);
        System.out.println(invoke1);
    }

}

 这里用到了invoke,Invoke的本质只是一个方法,方法一定是要通过对象来调用的。

6.如何获取类中构造方法对象。

Constructor 与Field、Method有所不同

构造方法不能被继承。父类的构造在子类中没有返回类型,方法名也与子类的类名不相同。不符合java语法规范。getConstructors() 只能获取到本类的public 构造方法。

1.getDeclaredConstructors()

获取本类所有 构造方法

//获取本类所有构造方法
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor constructor:declaredConstructors){
            System.out.println(constructor);
        }

2.getConstructors() 

获取到本类的public 构造方法

 //获取本类 public 构造方法
        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor constructor:constructors){
            System.out.println(constructor);
        }

3.getConstructor() 

//获取无参构造
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
        System.out.println(declaredConstructor);

4.getDeclaredConstructor()

 //获取有参构造
 Constructor<?> declaredConstructor1 = aClass.getDeclaredConstructor(Integer.class);
System.out.println(declaredConstructor1);

6.2Constructor常用方法。

public class Test5 {


    public static void main(String[] args) throws Exception {
        //反射获取类对象
        Class<?> aClass = Class.forName("com.reflect.Test.B");
        //获取类对象
        Object o = aClass.newInstance();

        //获取有参构造
        Constructor<?> declaredConstructor1 = aClass.getDeclaredConstructor(Integer.class);

        System.out.println(declaredConstructor1);
            declaredConstructor1.setAccessible(true);
        Object o1 = declaredConstructor1.newInstance(123);
    }
}



class A {
    public A(){
        System.out.println("父类无参构造");
    }
    public  A(String name){
        System.out.println("父类有参构造");
    }
}
class B extends A {
    public B(){
        System.out.println("子类无参构造");
    }
    public  B(String name){
        System.out.println("子类有参构造"+name);
    }
    private B(Integer age){
        System.out.println("子类有参构造"+age);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值