Java-反射

Java-反射

##1.什么反射:

先看正向操作,先有类,再根据类产生类的实例化对象;

反射就是根据得到类的Class对象,在更加Class对象实例化类

任何类的都有一个描述自己的Class类,记录他的构造方法,普通方法,属性等

2.取得类的Class对象

1.Object类里面的getClass()

public final native Class<?> getClass();

2.类.Class

3.通过Class里面的forName(“类的全名称”);

 public static Class<?> forName(String className)

Class类有newInstance方法可以实例化对象

public T newInstance()
    throws InstantiationException, IllegalAccessException

改造工厂方法:

interface Fruit{
    void buy();
}
class Apple implements Fruit{

    @Override
    public void buy() {
        System.out.println("买苹果");
    }
}
class Banana implements Fruit{

    @Override
    public void buy() {
        System.out.println("买香蕉");
    }
}
class Factory{
    //传入类的全名称,这样不管商品类如何增加都不用改工厂类
    public static Fruit getInstance(String classNmae) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Fruit fruit=null;
        Class cls=Class.forName(classNmae);
        fruit= (Fruit) cls.newInstance();
        cls.getConstructors();
        return fruit;
    }
}
public class Test{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Fruit fruit=Factory.getInstance("com.bit.reflect.Apple");
        fruit.buy();

    }
}

##3.取得类的构造方法Constructor

取得类构造方法的描述
public Constructor<?>[] getConstructors() throws SecurityException获取本类public的构造方法
public Constructor<?>[] getDeclaredConstructors() throws SecurityException获取本类声明的所有权限的构造方法
public Constructor getConstructor(Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException获取本类指定参数的public构造方法
public Constructor getDeclaredConstructor(Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException获得本类指定参数的所有权限的构造方法

**得到Constructor后可以根据 **

public T newInstance(Object … initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

这里是Constructor 类的方法

先定义Person和Student类,Student继承Person

class Person {
    private String name;
    public Integer age;
    public   static Integer ID;


    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    protected Person(Integer age) {
        this.age = age;
    }
    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    public  static void print(){

    }
}


class Student extends Person{
    private String school;
    private  Student() {
    }

    public Student(String school) {
        this.school = school;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
}

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
      Person per = new Person();
        /**
        三种取得Class对象的方法
        */
        Class cls1 = per.getClass();
        Class cls2 = Person.class;
        Class cls3 = Class.forName("com.bit.reflect.Student");
        
        /*
        	1.getDeclaredConstructors()所有权限
        */
        Constructor[] constructor1=cls1.getDeclaredConstructors();
        /*
        	2.getConstructors()
        */
         Constructor[] constructor2=cls1.getConstructors();
        /*
        	3.getConstructor()
        */
        Constructor constructor3 = cls1.getConstructor(String.class);
        /*
        	4.getDeclaredConstructor()
        */
        Constructor constructor4 = cls3.getDeclaredConstructor(String.class);
        Object obj=constructor4.newInstance()
    }
}

4.取得Method的对象

取得类方法的描述
public Method[] getDeclaredMethods() throws SecurityException取得本类声明的所有权限的方法包括static
public Method getDeclaredMethod(String name, Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException取得本类声明,指定名字的的任意权限的方法,包括static方法
public Method[] getMethods() throws SecurityException取得本类和父类public权限的方法 包括static
public Method getMethod(String name, Class<?>… parameterTypes) throws NoSuchMethodException, SecurityException取得本类和父类指定名字public权限的方法包括static
public Object invoke(Object obj, Object... args)
    throws IllegalAccessException, IllegalArgumentException,
       InvocationTargetException

invoke方法,取得方法

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
      Person per = new Person();
        /**
        三种取得Class对象的方法
        */
        Class cls1 = per.getClass();
        Class cls2 = Person.class;
        Class cls3 = Class.forName("com.bit.reflect.Student");
        Method method1=cls2.getMethod("getName");
        System.out.println(method1.invoke(object));
        Method method2=cls2.getMethod("getAge");
        System.out.println(method2.invoke(object));
    }
}

5.取得类的属性

取得类的属性描述
public Field[] getFields() throws SecurityException取得本类和父类的public权限的所有属性包括static
public Field getField(String name) throws NoSuchFieldException, SecurityException取得本类和父类指定名字public权限的属性包括static
public Field[] getDeclaredFields() throws SecurityException取得本类声明的,所有权限的属性 包括static
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException取得本类声明的,指定名字所有权限的属性,包括static

还有一组取得和设置属性的方法

取得值

public Object get(Object obj)
    throws IllegalArgumentException, IllegalAccessException

设置值

public void set(Object obj, Object value)
    throws IllegalArgumentException, IllegalAccessException
import java.io.File;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

      Person per = new Person();

        /**

        三种取得Class对象的方法

        */

        Class cls1 = per.getClass();
        Class cls2 = Person.class;
        Class cls3 = Class.forName("com.bit.reflect.Student");
        Object object=cls1.getConstructor(String.class,Integer.class).
                newInstance("hah",22);

        Field fields1=cls1.getDeclaredField("name");
        fields1.setAccessible(true);//由于name是私有的,所以无法直接取得所以要动态破坏封装性 ,再一次进程中有效
        System.out.println(fields1.get(object));

    }

}

public void setAccessible(boolean flag) throws SecurityException//动态破话封装性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值