Public Static voidMain (string[] args) {Try{Class CLA= Class.forName ("Com.money.test.Employee"); Object obj=cla.newinstance (); Method method2= Cla.getdeclaredmethod ("Setnum",int.class); System. out. println (Method2.invoke (obj,3)); Method[] Methods= CLA. Getdeclaredmethods (); for(Method method:methods) {System. out. println (Method.getname ()); if(Method.getname (). Contains ("Setnum") ) {System. out. println (Method.invoke (obj,2)); } } } Catch(Exception ex) {System. out. println (Ex.getstacktrace ()); }}
classEmployee {Private intsalary; Privatejava.util.Date Hireday; PrivateString name; Public intgetsalary () {returnsalary; } Public voidSetsalary (intsalary) { This. Salary =salary; } PublicDate Gethireday () {returnHireday; } Public voidSethireday (Date hireday) { This. Hireday =Hireday; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicEmployee (String name,intsalary, Date hireday) { This. Name =name; This. Hireday =Hireday; This. Salary =salary; } PublicEmployee () {}Static Privatehashmap<string, string> dict =NewHashmap<string, string>(); Static{num=0;/*dict.put ("1", "Employee"); Dict.put ("2", "Employee");*/} @Override PublicString toString () {if(Hireday = =NULL) Hireday=calendar.getinstance (). GetTime (); SimpleDateFormat formatter; Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); returnString.Format ("name:%s,salary:%d,hireday:%s", name, salary, Formatter.format (hireday)); } Static intnum; Public Static intGetnum () {returnnum; } Public Static intSetnum (inta) {num+=A; returnGetnum (); }}
One of the Java reflections