Java基础之反射

2020-03-15  本文已影响0人  HeloWxl

1、什么是Java反射?

Java反射就是在Java程序运行时,可以加载、探知、使用编译期间完全为未知的类。也就是说,Java程序可以加载一个运行时才得知类名的类,获得类的完整构造方法,并实例化初对象,给对象属性设定值或者调用对象的方法。

package com.ustcinfo.wangxianlin.reflection;
public class Cat {
    /**
     * 名字
    */
    public String name;
    /**
     * 颜色
     */
    private String color;
    /**
     * 品种
     */
    String type;

    /**
     * 无参构造方法
     */
    public Cat() {
    }

    /**
     * 有参构造方法
     */
    public Cat(String name, String color, String type) {
        this.name = name;
        this.color = color;
        this.type = type;
    }
    /**
     * 有参构造方法
     */
    public Cat(String name) {
        this.name = name;
    }

    /**
    * @Description:  吃饭
    * @params: []
    * @return: void
    */
    public void eat(){
        System.out.println("吃饭");
    }

    /**
     * @Description:  睡觉
     * @params: []
     * @return: void
     */
    private void sleep(){
        System.out.println("睡觉");
    }

    /**
     * getter setter方法
     */
}
package com.ustcinfo.wangxianlin.reflection;

import java.lang.reflect.Method;

public class Reflections {

    public static void main(String[] args) {

        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Method method[] = c.getDeclaredMethods();
            System.out.println("该类的方法有:");
            for (int i = 0 ;i <method.length;i++){
                System.out.println(method[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

这里的运行运行结果是将

2、Class类

2.1获取Class类的方法

Class.forName("java.lang.Object")
Class c = Cat.class
Class c = Integer.TYPE
String name = "美短"
Class c = name.getClass()
Class c = String.class.getSuperClass()

2.2 Class类常用方法

2.2.1 Filed[] getFileds()

返回一个包含Filed对象的数组,存放该类或接口的所有可访问公共属性(包含继承的公共属性)

public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Field[] fields = c.getFields();
            System.out.println("公共属性数量:"+fields.length);
            System.out.println("分别是:");
            for (int i = 0 ;i <fields.length;i++){
                System.out.println(fields[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

运行结果:


image.png

2.2.2 Filed[] getDeclaredFields()

返回一个包含Filed对象的数组,存放该类或接口的所有属性(不含继承的属性)

public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Field[] fields = c.getDeclaredFields();
            System.out.println("所有属性属性数量:"+fields.length);
            System.out.println("分别是:");
            for (int i = 0 ;i <fields.length;i++){
                System.out.println(fields[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

运行结果:


image.png

2.2.3 Filed[] getFiled()

返回一个指定公共属性名的Filed对象

   public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Field name = c.getField("name");
            System.out.println(name.toString());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

运行结果:


image.png

2.2.4 Method[] getMethods()

 public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
             Method[] methods = c.getMethods();
            System.out.println("所有可访问公共方法的数量:");
            System.out.println("分别是:");
            for(int i = 0;i<methods.length;i++){
                System.out.println(methods[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

运行结果:


image.png

2.2.5 Method[] getDeclaredMethods()

public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
             Method[] methods = c.getDeclaredMethods();
            System.out.println("所有方法的数量:"+methods.length);
            System.out.println("分别是:");
            for(int i = 0;i<methods.length;i++){
                System.out.println(methods[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

运行结果:


img.png

2.2.6 Constructor[] getConstructors()

 public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Constructor[] constructor = c.getConstructors();
            System.out.println("所有公共构造方法的数量:"+constructor.length);
            System.out.println("分别是:");
            for(int i = 0;i<constructor.length;i++){
                System.out.println(constructor[i].toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

2.2.7 Class[] getInterfaces()

public class Cat implements Readable
public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Class[] c1 = c.getInterfaces();
            for (Class cl:c1
                 ) {
                System.out.println(cl.getName());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

2.2.8 T newInterfaces()

 @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
public static void main(String[] args) {
        try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            Cat cat = (Cat) c.newInstance();
            cat.setColor("黑色");
            cat.setName("小花");
            cat.setType("美短");
            System.out.println(cat.toString());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

2.2.9 String getName()

以String的形式返回该类(类、接口、数组类、基本类型或void)的完整名.

try {
            Class c = Class.forName("com.ustcinfo.wangxianlin.reflection.Cat");
            System.out.println(c.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
image.png

推荐阅读

高级特性-反射

上一篇 下一篇

猜你喜欢

热点阅读