【发布时间】:2021-01-24 17:09:06
【问题描述】:
我希望有一个工厂继承另一个工厂,两个工厂都实现相同的通用接口,但类型不同,彼此继承:
class FactoryA implements FactoryI<A> {
// common code
...
A get() {
return new A();
}
}
class FactoryB extends FactoryA implements FactoryI<B> {
B get() {
return new B();
}
}
class B extends A {
}
FactoryI<T> {
T get()
}
但这给了我以下编译错误:
'FactoryI' cannot be inherited with different type arguments: 'A' and 'B'
我的第一个版本没有从 FactoryA 继承 FactoryB,这工作正常。 但事实证明,现在两个工厂之间有我想要重用的通用代码。
实现这一目标的最佳方法是什么?
【问题讨论】:
标签: java inheritance interface factory