4.查看类的成员

一个类的成员包括:

  • 变量
  • 构造器
  • 方法

在反射中,变量用Field对象表示,构造器用Constructor对象表示,方法则用Method表示。

Class中相关方法:

getConstructors();返回该类型的Class的所有构造器方法对象
getDeclaredFields();返回该类型Class的所有字段对象(包括私有的,不包括继承的)
getDeclaredMethods();返回该类型Class的所有方法对象(包括私有的,不包括继承的)
getFields();返回该类型Class的所有公共字段对象(包括超类的)
getMethoods();返回该类型Class的所有公共方法对象(包括超类的)

实例学习

首先编写测试用的Bean:

package ReflectStudy.Exa141;

import java.util.*;

/**
 * 我是测试的Bean
 */
@Deprecated
public class TestBean<String> extends AbstractList {
    public String name;
    public String code;
    private String password;

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }


    @Override
    public Object get(int index) {
        return null;
    }
}

然后编写程序:

package ReflectStudy.Exa142;

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

/**
 * 查看类的成员:字段,方法,构造方法
 */
public class ClassViewer {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("ReflectStudy.Exa141.TestBean");
        System.out.println("类的标准名称:" + clazz.getCanonicalName());
        Constructor[] constructors = clazz.getConstructors();// 获得该类对象的所有构造方法
        System.out.println("类的构造方法:");
        if (constructors.length != 0) {
            for (Constructor constructor : constructors) {
                System.out.println("\t" + constructor);// 输出构造方法
            }
        } else {
            System.out.println("\t空");
        }
        Field[] fields = clazz.getDeclaredFields();// 获得该类对象的所有非继承域
        System.out.println("类的非继承域变量:");
        if (fields.length != 0) {
            for (Field field : fields) {
                System.out.println("\t" + field);// 输出非继承域
            }
        } else {
            System.out.println("\t空");
        }
        Method[] methods = clazz.getDeclaredMethods();// 获得该类对象的所有非继承方法
        System.out.println("类的非继承方法:");
        if (methods.length != 0) {
            for (Method method : methods) {
                System.out.println(method);// 输出非继承方法
            }
        } else {
            System.out.println("\t空");
        }

        Field[] pubFields = clazz.getFields();
        Method[] pubMethods = clazz.getMethods();
        System.out.println("类的所有公共字段:");
        for (Field field : pubFields) {
            System.out.println("\t" + field);
        }
        System.out.println("类的所有公共方法:");
        for (Method method : pubMethods) {
            System.out.println(method);
        }
    }
}

运行之,测试结果:

"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.3\lib\idea_rt.jar=50311:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;D:\MyReposity\Study\study\Java动手做一做\target\classes;C:\Users\17761\.m2\repository\org\assertj\assertj-core\2.5.0\assertj-core-2.5.0.jar;C:\Users\17761\.m2\repository\mysql\mysql-connector-java\8.0.24\mysql-connector-java-8.0.24.jar;C:\Users\17761\.m2\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar" ReflectStudy.Exa142.ClassViewer
类的标准名称:ReflectStudy.Exa141.TestBean
类的构造方法:
	public ReflectStudy.Exa141.TestBean()
类的非继承域变量:
	public java.lang.Object ReflectStudy.Exa141.TestBean.name
	public java.lang.Object ReflectStudy.Exa141.TestBean.code
	private java.lang.Object ReflectStudy.Exa141.TestBean.password
类的非继承方法:
public java.lang.Object ReflectStudy.Exa141.TestBean.get(int)
public boolean ReflectStudy.Exa141.TestBean.contains(java.lang.Object)
public boolean ReflectStudy.Exa141.TestBean.isEmpty()
public int ReflectStudy.Exa141.TestBean.size()
类的所有公共字段:
	public java.lang.Object ReflectStudy.Exa141.TestBean.name
	public java.lang.Object ReflectStudy.Exa141.TestBean.code
类的所有公共方法:
public java.lang.Object ReflectStudy.Exa141.TestBean.get(int)
public boolean ReflectStudy.Exa141.TestBean.contains(java.lang.Object)
public boolean ReflectStudy.Exa141.TestBean.isEmpty()
public int ReflectStudy.Exa141.TestBean.size()
public void java.util.AbstractList.add(int,java.lang.Object)
public boolean java.util.AbstractList.add(java.lang.Object)
public java.lang.Object java.util.AbstractList.remove(int)
public boolean java.util.AbstractList.equals(java.lang.Object)
public int java.util.AbstractList.hashCode()
public int java.util.AbstractList.indexOf(java.lang.Object)
public void java.util.AbstractList.clear()
public java.util.Iterator java.util.AbstractList.iterator()
public int java.util.AbstractList.lastIndexOf(java.lang.Object)
public java.util.List java.util.AbstractList.subList(int,int)
public boolean java.util.AbstractList.addAll(int,java.util.Collection)
public java.lang.Object java.util.AbstractList.set(int,java.lang.Object)
public java.util.ListIterator java.util.AbstractList.listIterator(int)
public java.util.ListIterator java.util.AbstractList.listIterator()
public boolean java.util.AbstractCollection.remove(java.lang.Object)
public java.lang.String java.util.AbstractCollection.toString()
public java.lang.Object[] java.util.AbstractCollection.toArray(java.lang.Object[])
public java.lang.Object[] java.util.AbstractCollection.toArray()
public boolean java.util.AbstractCollection.addAll(java.util.Collection)
public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
public boolean java.util.AbstractCollection.removeAll(java.util.Collection)
public boolean java.util.AbstractCollection.retainAll(java.util.Collection)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default java.util.stream.Stream java.util.Collection.stream()
public default boolean java.util.Collection.removeIf(java.util.function.Predicate)
public default java.util.stream.Stream java.util.Collection.parallelStream()
public default void java.lang.Iterable.forEach(java.util.function.Consumer)
public default void java.util.List.replaceAll(java.util.function.UnaryOperator)
public default java.util.Spliterator java.util.List.spliterator()
public default void java.util.List.sort(java.util.Comparator)

Process finished with exit code 0
上一篇:在Linux命令行提示符PS1中优雅地显示日期和时间


下一篇:javaweb-Thymeleaf-ssti