代码
public class FacTest {
public void one ( String name ) {
System. out. println ( "one one one" + name) ;
}
public void two ( String name, Integer age ) {
System. out. println ( "two two two" + name + age) ;
}
public void three ( List< String> list ) {
for ( String s : list) {
System. out. println ( s) ;
}
}
public void four ( String... strs ) {
for ( Object object : strs) {
System. out. println ( object) ;
}
}
public void test ( ) throws Exception {
Class< ? > facTest = Class. forName ( "com.hy.emun.FacTest" ) ;
Method one = facTest. getDeclaredMethod ( "one" , String. class) ;
one. invoke ( facTest. newInstance ( ) , "小三" ) ;
Method two = facTest. getDeclaredMethod ( "two" , String. class, Integer. class) ;
two. invoke ( facTest. newInstance ( ) , "小四" , 12 ) ;
Method three = facTest. getDeclaredMethod ( "three" , List. class) ;
three. invoke ( facTest. newInstance ( ) , Arrays. asList ( "name" , "age" ) ) ;
}
public static void main ( String[ ] args) throws Exception {
new FacTest ( ) . test ( ) ;
new FacTest ( ) . four ( "a" , "b" , "c" ) ;
}
}
结果
one one one小三
two two two小四12
name
age
a
b
c