我有密码:
1 abstract class A {
2 public abstract <T> T getId();
3 }
4 class B extend A {
5 public Long getId() {
6 return Long.valueOf(1);
7 }
8 }
9 public class Main {
10 public static void main(String [] args) {
11 FastClass fast = FastClass.create(B.class);
12 FastMethod fastMethod = fast.getMethod("getId", null);
13 try {
14 final B b = new B();
15 Long value = (Long) fastMethod.invoke(b, null);
16 } catch (Exception e) {//
17 e.printStackTrace();
18 }
19 }
20 } 并在第12行抛出IllegalArgumentException:无法找到方法公共java.lang.Long B.getId()。
如何获得实例FastMethod类并执行FastMethod.invoke()?
发布于 2013-09-17 04:56:53
我必须为类A获取实例FastClass,并从它获取实例FastMethod,但是使用参数实例类B调用实例FastMethod。
此代码运行良好:
1 abstract class A {
2 public abstract <T> T getId();
3 }
4 class B extend A {
5 public Long getId() {
6 return Long.valueOf(1);
7 }
8 }
9 public class Main {
10 public static void main(String [] args) {
11 FastClass fast = FastClass.create(A.class); // changed, was FastClass.create(B.class)
12 FastMethod fastMethod = fast.getMethod("getId", null);
13 try {
14 final B b = new B();
15 Long value = (Long) fastMethod.invoke(b, null);
16 } catch (Exception e) {
17 e.printStackTrace();
18 }
19 }
20 } https://stackoverflow.com/questions/18786826
复制相似问题