您的位置:首页 > 其它

使用反射调用一个类的方法

2017-08-14 19:57 218 查看
下面用例子程序来说明:

import java.lang.reflect.Method;

public class WhtTest {
public static void main( String[] args ) throws Exception {
Count c = new Count( 2, 3 );
Method m = Count.class.getMethod( "plus" );
System.out.println( m.invoke( c ) ); // 5
}

}

class Count {
int a;

int b;

public Count( int a, int b ) {
this.a = a;
this.b = b;
}

public int plus() {
return this.a + this.b;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐