面试题中遇到的问题,相信各位小伙伴,在面试中遇到过各类神奇的问题,不是各位说不会,也可能是没有实际应用过这类的方法,所以在面试时非常吃亏,给面试官的印象也不好。在此记录下来,与各位共勉!!
如何在List
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
List list = new ArrayList();
Class> cls = list.getClass();
Method method = cls.getMethod("add", Object.class);
method.invoke(list, 1);
method.invoke(list, "s");
method.invoke(list, 2);
method.invoke(list, 4);
System.out.println(list);
ArrayList
而且这种应用直接跳过编译!