利用反射——查看类的成员
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用反射——查看类的成员相关的知识,希望对你有一定的参考价值。
在一个类的内部,一般包括成员变量、构造方法、普通方法和内部类等
public class ClassViewer { @SuppressWarnings("rawtypes") public static void main(String[] args) throws ClassNotFoundException { Class<?> clazz = Class.forName("java.util.ArrayList"); 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("\t"+method); } }else { System.out.println("\t空"); } } }
输出结果如下:
类的标准名称:java.util.ArrayList 类的构造方法: public java.util.ArrayList(java.util.Collection) public java.util.ArrayList() public java.util.ArrayList(int) 类的非继承域变量: private static final long java.util.ArrayList.serialVersionUID private static final int java.util.ArrayList.DEFAULT_CAPACITY private static final java.lang.Object[] java.util.ArrayList.EMPTY_ELEMENTDATA private transient java.lang.Object[] java.util.ArrayList.elementData private int java.util.ArrayList.size private static final int java.util.ArrayList.MAX_ARRAY_SIZE 类的非继承方法: public void java.util.ArrayList.add(int,java.lang.Object) public boolean java.util.ArrayList.add(java.lang.Object) public java.lang.Object java.util.ArrayList.remove(int) public boolean java.util.ArrayList.remove(java.lang.Object) public java.lang.Object java.util.ArrayList.get(int) public java.lang.Object java.util.ArrayList.clone() public int java.util.ArrayList.indexOf(java.lang.Object) public void java.util.ArrayList.clear() public boolean java.util.ArrayList.contains(java.lang.Object) public boolean java.util.ArrayList.isEmpty() public int java.util.ArrayList.lastIndexOf(java.lang.Object) public int java.util.ArrayList.size() public java.util.List java.util.ArrayList.subList(int,int) public java.lang.Object[] java.util.ArrayList.toArray() public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[]) static int java.util.ArrayList.access$100(java.util.ArrayList) static java.lang.Object[] java.util.ArrayList.access$200(java.util.ArrayList) public boolean java.util.ArrayList.addAll(java.util.Collection) public boolean java.util.ArrayList.addAll(int,java.util.Collection) public java.util.Iterator java.util.ArrayList.iterator() private void java.util.ArrayList.readObject(java.io.ObjectInputStream) throws java.io.IOException,java.lang.ClassNotFoundException private void java.util.ArrayList.writeObject(java.io.ObjectOutputStream) throws java.io.IOException public java.lang.Object java.util.ArrayList.set(int,java.lang.Object) public void java.util.ArrayList.ensureCapacity(int) public void java.util.ArrayList.trimToSize() private void java.util.ArrayList.ensureCapacityInternal(int) java.lang.Object java.util.ArrayList.elementData(int) private void java.util.ArrayList.grow(int) private static int java.util.ArrayList.hugeCapacity(int) public java.util.ListIterator java.util.ArrayList.listIterator(int) public java.util.ListIterator java.util.ArrayList.listIterator() public boolean java.util.ArrayList.removeAll(java.util.Collection) protected void java.util.ArrayList.removeRange(int,int) public boolean java.util.ArrayList.retainAll(java.util.Collection) private java.lang.String java.util.ArrayList.outOfBoundsMsg(int) private void java.util.ArrayList.rangeCheckForAdd(int) private boolean java.util.ArrayList.batchRemove(java.util.Collection,boolean) private void java.util.ArrayList.ensureExplicitCapacity(int) private void java.util.ArrayList.fastRemove(int) private void java.util.ArrayList.rangeCheck(int) static void java.util.ArrayList.subListRangeCheck(int,int,int)
本文出自 “IT菜鸟” 博客,请务必保留此出处http://mazongfei.blog.51cto.com/3174958/1908119
以上是关于利用反射——查看类的成员的主要内容,如果未能解决你的问题,请参考以下文章