反射机制应用很广泛。这里简单备注下
package com.shone.ailin;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Reflectdemo { public static void main(String[] args) throws Exception { printVar(); createInstance(); printAllVarName(); } //打印所有成员变量名 private static void printAllVarName() { // TODO Auto-generated method stub Point p = new Point(1,3); Field[] fields = p.getClass().getDeclaredFields(); for(int i=0;i
Point类
package com.shone.ailin;public class Point { private int x ; public int y ; public Point(int x ,int y ) { // TODO Auto-generated constructor stub super(); this.x = x; this.y = y; } private int addCount(int x,int y) { return x+y; }}