首页 > 其他分享 >【反射】反射基本使用

【反射】反射基本使用

时间:2022-10-03 10:22:07浏览次数:52  
标签:基本 反射 System class public 使用 println Class out

1.获取类信息

class Student {
    static {
        System.out.println("加载Student类");
    }

    public Student() {
        System.out.println("Construct Student");
    }
}

public class ClassTest {
    public static void main(String[] args) throws ClassNotFoundException {
        Integer integer = 5;

        //1. 通过.class的方式获取Class实例
        Class<Integer> c1 = int.class;
        System.out.println("c1= " + c1);

        Class<Integer> c2 = Integer.class;
        System.out.println("c2= " + c2);

        //2. 基本类型的包装类通过.Type获取相对应基本数据类型的Class实例
        Class<Integer> c3 = Integer.TYPE;
        System.out.println("c3 = " + c3);

        //3. 利用对象实例调用getClass方法获取该对象的Class实例
        Class<? extends Integer> c4 = integer.getClass();
        System.out.println("c4 = " + c4);

        //4. 利用对象调用getClass方法获取该对象的Class实例
        Class<Student> c5 = Student.class;
        System.out.println("c5= " + c5);

        //5. 使用Class类的静态方法forName通过全类名获取Class实例
        Class<?> c6 = Class.forName("Student");
        System.out.println("c6= " + c6);


    }
}

2.获取方法和字段信息

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

class MyClass {
    public int pubField;
    protected int proField;
    private int priField;

    public MyClass() {
    }

    public MyClass(int a) {
    }

    protected MyClass(int a, int b) {
    }

    private MyClass(int a, int b, int c) {
    }

    public void pub_method() {
    }

    protected void pro_method() {
    }

    void defMethod() {
    }

    private void priMethod() {
    }

    public static void staticMethod() {
    }

}

interface MyInterface {
    float pi = 3.14f;

    void fun();

    default void defFun() {
    }

    static void staticFun() {
    }
//    private void  priFun(){}
}

public class ClassInfo {
    public static void main(String[] args) {
        Class clz = MyClass.class;
        System.out.println("Fields:");
        //只获取公共字段
        for (Field field : clz.getFields()) {
            System.out.println(field);
        }
        System.out.println("---------------------------------");
        System.out.println("Constructors:");
        //获取所有声明的构造器
        for (Constructor<?> constructor : clz.getDeclaredConstructors()) {
            System.out.println(constructor);
        }
        System.out.println("---------------------------------");
        System.out.println("Methods:");
        //只获取公共方法,包括从Object继承的公共方法
        for (Method method : clz.getMethods()) {
            System.out.println(method);
        }
        System.out.println("---------------------------------");
        clz = MyInterface.class;
        System.out.println("Interface's Methods:");
        //只获取接口中的公共方法
        for (Method method : clz.getMethods()) {
            System.out.println(method);
        }
    }
}

3.获取基类和接口信息

interface A {
}

interface B {
}

class Base {
}

class Derived extends Base implements A, B {
}

public class SuperClassAndInterface {
    public static void main(String[] args) {
        Class<Derived> clz = Derived.class;
        Class<? super Derived> baseClz = clz.getSuperclass();
        System.out.println("基类:" + baseClz);
        Class<?>[] interfaces = clz.getInterfaces();
        for (Class<?> c : interfaces) {
            System.out.println(c);
        }
    }
}

4.获取枚举信息

enum Week {
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

public class EnumInfo {
    public static void main(String[] args) {
        Class<Week> clz = Week.class;
        Week[] weeks = clz.getEnumConstants();
        for (Week week : weeks) {
            System.out.println(week);
        }
    }
}

5.获取泛型信息

import java.lang.reflect.Type;
import java.util.ArrayList;

interface Functor<T> {
    void call(T args);
}

class MyCallBack implements Functor<Object> {

    @Override
    public void call(Object args) {
        System.out.println(args);
    }
}

class CallbackTest {
    public static <T> T callback(ArrayList<T> list, Functor<? super T> fun) {
        for (T each : list) {
            fun.call(each);
        }
        return list.get(0);
    }
}

public class GenericInfo {
    public static void main(String[] args) {
        System.out.println("[MyCallBack类的泛型信息]");
        Class clz = MyCallBack.class;
        Type baseType = clz.getGenericSuperclass();
        System.out.println("基类:");
        System.out.println(baseType);
    }
}

标签:基本,反射,System,class,public,使用,println,Class,out
From: https://www.cnblogs.com/xl4ng/p/16750102.html

相关文章