java-反射自我記憶

Persson對象

package Reflect;

public class Person {
    private  String name;
    private  int age;
    public  String a;
    protected   String b;
        String c;
    private   String d;

    public Person() {
    }

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

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

    private Person(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", a='" + a + '\'' +
                ", b='" + b + '\'' +
                ", c='" + c + '\'' +
                ", d='" + d + '\'' +
                '}';
    }

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

    private void sleep(){
        System.out.println("1");
    }
    private  void sleep(String s){
        System.out.println("2");
    }

    public void eat(){
        System.out.println("3");
    }
    public void eat(String s){
        System.out.println("4");
    }
}

 

一、通過反射獲取class對象

public class ReflectTest1 {
    /**
     * 獲取Class
     */
    @Test
    public void getClassObject() throws Exception {
        //1.Class.forName通過方法名
      Class calzz=  Class.forName("Reflect.Person");
      System.out.println(calzz);
        //2.通過類名,Calss
        Class calzz2=Person.class;
        System.out.println(calzz2);
        //3.通過對象名.class
        Person person=new Person();
        Class clazz3=person.getClass();
        System.out.println(clazz3);

        if(calzz==calzz2&&calzz==clazz3){
            System.out.println("相等");
        }
    }
}

獲取Class對象

 Class clazz;

    {
        try {
            clazz = Class.forName("Reflect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

 

二、通過反射獲取成員變量

 /**
     * 1.獲取成員變量
     * Field[] getFileds()
     * Field getField(String name)
     * getDeclaredField()
     * getDeclaredFields()
     */
    @Test
    public void getMygetFileds() throws Exception {
        System.out.println("-------------------獲取所有的公開成員變量--------------------------");
        //獲取所有public修飾成員變量
        Field[] file = clazz.getFields();
        for (int i = 0; i < file.length; i++) {

            System.out.println(file[i]);
        }
        System.out.println("-------------------通過變量名獲取成員變量--------------------------");
        Field a = clazz.getField("a");
        Person p = new Person();
        Object object = a.get(p);
        System.out.println(object);
        //設置ad的值
         a.set(p,"a");
          System.out.println(p);
        System.out.println("-------------------獲取所有的成員變量--------------------------");
      Field fields[]=clazz.getDeclaredFields();

      for(int i=0;i<fields.length;i++){
          System.out.println(fields[i]);
      }
        System.out.println("-------------------通過名字來獲取成員變量--------------------------");
       Field field1=clazz.getDeclaredField("d");
       System.out.println(field1);

    }

三、通過反射獲取構造方法

/** *2獲取構造方法
     * <p>
     * <p>
     * Constructor<?>[] getConstructors()
     * Constructor<T> getConstructor(String name)
     * <p>
     * Constructor<?>[] getDeclaredConstructors()
     * Constructor<?> getDeclaredConstructors(String name)
     * <p>*/
   //通過反射獲取構造方法
    @Test
    public void getMygetConstructor() throws Exception {
        System.out.println("-------------------獲取所有的公開構造方法--------------------------");
        //獲取所有public構造方法
         Constructor[] constructors=clazz.getConstructors();
        for (int i=0;i<constructors.length;i++) {
         System.out.println(constructors[i]);
        }
        System.out.println("-------------------獲取指定參數公開的構造方法--------------------------");
       //獲取public指定對象的構造方法
        Constructor constructor=clazz.getConstructor(String.class,int.class);
        System.out.println(constructor);
        //通過構造方法創建一個對象
        System.out.println("-------------------通過公開的構造方法創建一個對象--------------------------");
        Object obj=constructor.newInstance("張三",44);
        System.out.println(obj.toString());
        //獲取所有的構造方法
        System.out.println("-------------------獲取所有的構造方法創建一個對象--------------------------");
         Constructor constructors1[]=clazz.getDeclaredConstructors();
         for (int i=0;i<constructors1.length;i++) {
             System.out.println(constructors1[i]);
         }
        System.out.println("-------------------獲取一個私有的構造方法--------------------------");
        //獲取指定構造方法
        Constructor constructor1 =clazz.getDeclaredConstructor(String.class);
         System.out.println(constructor1);
        System.out.println("-------------------通過一個私有的方法創建一個對象--------------------------");
        constructor1.setAccessible(true);
        Object obj1=constructor1.newInstance("qqqq");

        System.out.println(obj1);
    }

四、通過反射獲取所有方法

/通過反射獲取所有的方法
      /** 3.獲取所有的方法
     * Method[] getMethods()
     * Method getMethod(String name)
     * <p>
     * Method[] getDeclaredMethods()
     * Method getDeclaredMethod(String name)*/
    @Test
    public void getMethods() throws Exception {
        System.out.println("-------------------獲取所有公開的方法--------------------------");
        Method [] methods=clazz.getMethods();
        for (int i=0;i<methods.length;i++){
            System.out.println(methods[i]);
        }
        System.out.println("-------------------通過名字過去一個公開的方法--------------------------");
        Method method=clazz.getMethod("getName");
        System.out.println(method);

        System.out.println("-------------------獲取所有的方法--------------------------");
        Method methods1[]=clazz.getDeclaredMethods();
        for (int i=0;i<methods1.length;i++){
            System.out.println(methods1[i]);
        }
        System.out.println("-------------------通過一個名字獲得一個私有的方法--------------------------");
        Method method1=clazz.getDeclaredMethod("sleep");
        System.out.println(method1);
        //運行一個方法
        System.out.println("-------------------運行一個私有的方法-------------------------");
        method1.setAccessible(true);
        Person p=new Person();
        method1.invoke(p);
        System.out.println("-------------------運行一個私有的方法-------------------------");
        Method method2=clazz.getDeclaredMethod("sleep",String.class);
        System.out.println(method1);
        method2.setAccessible(true);
        method2.invoke(p,"aaa");
    }

五、獲取類名

    @Test
   public  void getData(){
     String className=clazz.getName();
      System.out.println(className);

   }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章