0

我有代码:

 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: Cannot find method public java.lang.Long B.getId()

如何获取实例 FastMethod 类并执行 FastMethod.invoke()?

4

1 回答 1

0

我必须为类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  } 
于 2013-09-17T04:56:53.817 回答