Java设计模式——反射 实例教学
反射就是为了获取已知民称的类或已有对象相关信息的一种机制。
以下就是实例,解释在代码中!要用到的就有Class,Constructor,Field,Method类
上代码:
1 package com.Mark.chapter01.reflectExercise;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
6
7 public class getMethodFromVector
8 {
9 public static void main(String[] args) throws ClassNotFoundException
10 {
11 Class classInfo=Class.forName("java.util.Vector");
12 // 取出构造方法
13 Constructor constructor[]=classInfo.getConstructors();
14 System.out.println("指定类构造函数如下:");
15 for(int i=0;i<constructor.length;i++)
16 {
17 System.out.println(constructor[i].toString());
18 }
19 System.out.println("");//空一行
20 // 取出类的成员变量(类的属性)
21 Field field[]=classInfo.getDeclaredFields();//注意这个declared
22 System.out.println("指定类变量如下:");
23 for(int i=0;i<field.length;i++)
24 {
25 System.out.println(field[i].toString());
26 }
27 System.out.println("");//空一行
28 // 取出类的方法
29 Method method[]=classInfo.getDeclaredMethods();//注意这个declared 同上 (解释在下)
30 // 1:只有父类属性无法获得;2:其他都能获得(包含private方法)
31 //如果不加declared,那么就无法得到类中private,protected的方法或变量
32 System.out.println("指定类的方法如下");
33 for(int i=0;i<method.length;i++)
34 {
35 System.out.println(method[i].toString());
36 }
37
38 }
39 }
运行结果:
指定类构造函数如下:
public java.util.Vector(java.util.Collection)
public java.util.Vector()
public java.util.Vector(int)
public java.util.Vector(int,int)
指定类变量如下:
protected java.lang.Object[] java.util.Vector.elementData
protected int java.util.Vector.elementCount
protected int java.util.Vector.capacityIncrement
private static final long java.util.Vector.serialVersionUID
private static final int java.util.Vector.MAX_ARRAY_SIZE
指定类的方法如下
public synchronized boolean java.util.Vector.add(java.lang.Object)
public void java.util.Vector.add(int,java.lang.Object)
public synchronized java.lang.Object java.util.Vector.remove(int)
public boolean java.util.Vector.remove(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.get(int)
public synchronized boolean java.util.Vector.equals(java.lang.Object)
public synchronized java.lang.String java.util.Vector.toString()
public synchronized int java.util.Vector.hashCode()
public synchronized java.lang.Object java.util.Vector.clone()
public synchronized int java.util.Vector.indexOf(java.lang.Object,int)
public int java.util.Vector.indexOf(java.lang.Object)
public void java.util.Vector.clear()
public boolean java.util.Vector.contains(java.lang.Object)
public synchronized boolean java.util.Vector.isEmpty()
public synchronized java.util.Iterator java.util.Vector.iterator()
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object,int)
public synchronized int java.util.Vector.lastIndexOf(java.lang.Object)
public synchronized void java.util.Vector.replaceAll(java.util.function.UnaryOperator)
public synchronized int java.util.Vector.size()
public synchronized java.util.List java.util.Vector.subList(int,int)
public synchronized java.lang.Object[] java.util.Vector.toArray()
public synchronized java.lang.Object[] java.util.Vector.toArray(java.lang.Object[])
public java.util.Spliterator java.util.Vector.spliterator()
public synchronized boolean java.util.Vector.addAll(int,java.util.Collection)
public synchronized boolean java.util.Vector.addAll(java.util.Collection)
public synchronized void java.util.Vector.addElement(java.lang.Object)
public synchronized java.lang.Object java.util.Vector.elementAt(int)
private void java.util.Vector.writeObject(java.io.ObjectOutputStream) throws java.io.IOException
public java.util.Enumeration java.util.Vector.elements()
public synchronized void java.util.Vector.forEach(java.util.function.Consumer)
public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)
public synchronized int java.util.Vector.capacity()
public synchronized void java.util.Vector.ensureCapacity(int)
public synchronized void java.util.Vector.trimToSize()
private static int java.util.Vector.hugeCapacity(int)
public synchronized void java.util.Vector.copyInto(java.lang.Object[])
public synchronized boolean java.util.Vector.containsAll(java.util.Collection)
java.lang.Object java.util.Vector.elementData(int)
private void java.util.Vector.ensureCapacityHelper(int)
public synchronized java.lang.Object java.util.Vector.firstElement()
private void java.util.Vector.grow(int)
public synchronized void java.util.Vector.insertElementAt(java.lang.Object,int)
public synchronized java.lang.Object java.util.Vector.lastElement()
public synchronized java.util.ListIterator java.util.Vector.listIterator()
public synchronized java.util.ListIterator java.util.Vector.listIterator(int)
public synchronized boolean java.util.Vector.removeAll(java.util.Collection)
public synchronized void java.util.Vector.removeAllElements()
public synchronized boolean java.util.Vector.removeElement(java.lang.Object)
public synchronized void java.util.Vector.removeElementAt(int)
public synchronized boolean java.util.Vector.removeIf(java.util.function.Predicate)
protected synchronized void java.util.Vector.removeRange(int,int)
public synchronized boolean java.util.Vector.retainAll(java.util.Collection)
public synchronized void java.util.Vector.setElementAt(java.lang.Object,int)
public synchronized void java.util.Vector.setSize(int)
public synchronized void java.util.Vector.sort(java.util.Comparator)
谢谢参考。