日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

编写Java程序 堆栈的接口_java - 错误:调用实现接口的通用方法时,java.lang.AbstractMethodError - 堆栈内存溢出...

發布時間:2025/3/12 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写Java程序 堆栈的接口_java - 错误:调用实现接口的通用方法时,java.lang.AbstractMethodError - 堆栈内存溢出... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在嘗試使用javassist以編程方式創建和編譯實現接口的類(在運行時)。

每當我調用該動態類的實例時,都會收到以下錯誤消息:

java.lang.AbstractMethodError: FooImpl.test()Ljava/lang/Object;

這是我的界面

public class FooBarInterface {

public T getEntity();

}

這是一個示例實體

public class FooEntity {

@Override

public String toString() {

return "Hello, Foo!";

}

}

這是我以編程方式實現接口的方式

public void test() {

ClassPool classPool = ClassPool.getDefault();

CtClass testInterface = classPool.get(FooBarInterface.class.getName());

CtClass fooImpl = classPool.makeClass("FooImpl");

fooImpl.addInterface(testInterface);

CtMethod testMethod = CtNewMethod.make(

"public com.test.FooEntity getEntity(){" +

"return new com.test.FooEntity();" +

"}",

canImpl

);

fooImpl.addMethod(testMethod);

fooImpl.writeFile();

TestInterface test =

(TestInterface) fooImpl.toClass().newInstance();

System.out.println(test.getEntity());

}

如果我將實現的方法的返回類型更改為Object,則不會收到錯誤,如下所示:

CtMethod testMethod = CtNewMethod.make(

"public Object getEntity(){" +

"return new com.test.FooEntity();" +

"}",

canImpl

);

然后我成功地打了hello, Foo! 。 我可以將返回類型更改為Object,但是我想進一步了解為什么使用Foo類型返回會產生AbstractMethodError 。

總結

以上是生活随笔為你收集整理的编写Java程序 堆栈的接口_java - 错误:调用实现接口的通用方法时,java.lang.AbstractMethodError - 堆栈内存溢出...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。