java 笔记 :创建anonymous类的实例

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class Test_Anonymous {

	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		Object o = new IRun(){
			public void run() {
				System.out.println("in anonymouse.run()");
			}
		};
		System.out.println("class = " + o.getClass().getName());
		Method[] methods= o.getClass().getMethods();
		for (int i=0;i<methods.length;i++) {
			System.out.println("method " + methods[i].getName());
		}
		//call the method
		o.getClass().getMethod("run").invoke(o);
	}//main
}//class

interface IRun {
	void run();
}

posted on 2012-03-04 21:08  learner  阅读(313)  评论(0)    收藏  举报

导航