不知道為什么報錯。。。
import?java.lang.reflect.Method; public?class?MethodDemo1?{ class?A{ public?void?print(int?a,int?b){ System.out.println(a+b); } public?void?print(String?a,String?b){ System.out.println(a.toUpperCase()+"?"+b.toLowerCase()); } } public?static?void?main(String[]?args){ A?a1?=?new?A(); Class?c?=?a1.getClass(); //獲取方法,名稱和參數(shù)列表決定 //getMethod獲取的是public的方法 //getDelcareMethod自己聲明的方法 try?{ //Method?m?=?c.getMethod("print",?new?Class[]{int.class,int.class}); Method?m?=?c.getMethod("print",int.class,int.class); //方法的反射操作,是用m對象進行方法調(diào)用 m.invoke(a1,?new?Object[]{10,20}); }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); }? } }
A a1 = new A(); ?這里報錯不知道為什么
No enclosing instance of type MethodDemo1 is accessible. Must qualify the allocation with an enclosing instance of type MethodDemo1 (e.g. x.new A() where x is an instance of MethodDemo1).
這是報錯的信息
求解答 ? 實在不知道怎么回事
2017-08-24
內(nèi)部類的實例化,需要先實例化外部類
把Class A{}? 放到ClassMethod1外邊去
2019-08-03
內(nèi)部類好像需要先實例化外部類