使用反射调用一个类的方法
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;
}
}
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;
}
}
相关文章推荐
- 私有方法是封闭的?使用反射来调用一个对象的私有方法。
- 使用反射-动态创建对象及调用对象方法
- [c#美味] 使用反射动态创建实例并调用方法
- 反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
- 一个小java反射程序,简单的调用方法。
- 深度复制一个对象方法。利用反射机制,不使用Serialization,也不使用ICloneable接口
- 使用反射-动态创建对象及调用对象方法
- java利用反射来调用一个类的私有方法
- 使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下(摘抄于MSDN):
- 在C#中使用反射调用internal的方法
- 使用反射调用方法
- 使用反射技术,为DataGridView增加一个自动填充的扩展方法
- 使用反射依导航路径调用对象成员方法,或返回对象子、孙属性或字段成员[转]
- 使用extjs,来实现一个页面调用另一个页面中的方法,并且能获得方法的返回值
- 反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
- 在使用JS调用Window.close()方法关闭一个窗口是,如何禁止弹出对话窗口?
- java反射调用配置文件构造类对象,使用其方法
- 使用反射令2个事件在不知道方法名的情况下使用同一个处理方法
- 使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下(摘抄于MSDN)
- 一个用jmail发信的过程,及使用方法.使用时直接调用就行