![](/img/trans.png)
[英]Why a generic method of an interface can be implemented as non-generic in Java?
[英]interface for generic method after method implemented
我目前的情況是:
module2 中實現的方法簽名如下所示:
Class MyClass{
public <T extends Enum<T> & Interface1> Interface2 myMethod(param1, param2...){.....}
}
Interface1 和 Interface2 是 module3 的一部分(我不能修改並且 module1 不能將其作為依賴項)
問題是:如何在 module1 中創建一個接口,由 MyClass 表單模塊 2 實現,並具有所需的方法簽名?
就像是:
模塊1:
interface MyInterface{
<T extends Enum<T> & -->What I should put here<--- > -->What I should put here<--- myMethod(param1, param2...){.....}
}
class MyClass implements MyInterface{
public <T extends Enum<T> & Interface1> Interface2 myMethod(param1, param2...){.....}
}
class MyClass2 implements MyInterface{
public <T extends Enum<T> & -->Different interface needed here than Interface1<-- > -->Different interface needed here than Interface2<-- myMethod(param1, param2...){.....}
}
有任何想法嗎? 我如何克服從 module3 繼承的接口(我無法更改也不能將其作為 module1 中的依賴項,只有在 module2 中我可以將其作為依賴項)基本上是在 module1 中提供更通用的接口方法簽名定義以滿足module2中方法的簽名?
目前它說:兩種方法:接口方法和 class 實現的方法具有相同的擦除,但都沒有覆蓋另一個。
使用推理:
<A, B, T extends Enum<T> & A> B myMethod(param1, param2...){.....}
並讓呼叫站點弄清楚。
恕我直言,當事情變得如此粗糙時,generics 被高估了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.