这有点难以解释,但我到处寻找,我找不到任何好的答案。
我也看到了Stack Overflow问题 How can I refer to the class type a interface is implementing in Java? 和 How do I return an instance of an object of the same type as the class passed in using Java 6? ,但他们无法回答我的问题。我申请继承时有一个例外。
有一个例子,让它更容易理解:
我们说我有一些名为SelfMaker的界面:
public interface SelfMaker <SELF>{
public SELF getSelf();
}
A有一只狗,可以与另一只狗生育。因此,狗是一个&#34; SelfMaker&#34;,就像这样:
public class Dog implements SelfMaker<Dog> {
String color;
public String toString() {
return "some " + color + " dog";
}
public Dog procreate(Dog anotherDog) {
Dog son = getSelf();
son.color = color;
return son;
}
@Override
public Dog getSelf() {
return new Dog();
}
}
然而,我有一只DomesticDog,他是一只狗,但它有一个可爱的家庭为他命名。像这样:
public class DomesticDog extends Dog {
private String name;
public String toString() {
return super.toString() + " named " + name;
}
}
现在,我有一些课程可以处理一些事情,这些事情是&#34; SelfMaker&#34; s,让我们称这个课程为#34;情侣&#34;。像这样:
public class Couple<T extends SelfMaker<T>> {
private T first;
private T second;
public String toString() {
return first.toString() + " and " + second.toString();
}
}
EXCEPTION:
当我想创建几个DomesticDog
时会出现异常。像这样:
public class CoupleOfDomesticDogs extends Couple<DomesticDog>{
public DomesticDog procreate(){
DomesticDog son = first.procreate(second);
return son;
}
}
这会在<DomesticDog>
抱怨Bound mismatch: The type DomesticDog is not a valid substitute for the bounded parameter <T extends SelfMaker<T>> of the type Couple<T>
我已经尝试将通用变量从类Couple更改为:Couple<T extends SelfMaker<?>>
但是&#34; son&#34;不会成为DomesticDog(我希望&#34;儿子&#34;成为DomesticDog)。如果我添加一些演员,那么它将编译,但它将不太清晰。
所以...这里有一个问题:如果没有演员和概括,有没有办法实现这个目标?
答案 0 :(得分:7)
如果没有施法,我无法想到这样做。如果您覆盖DomesticDog的procreate和getSelf方法并更改类Couple的声明,那么您的问题将得到解决:
public class DomesticDog extends Dog {
private String name;
public DomesticDog procreate(Dog anotherDog) {
return (DomesticDog)super.procreate(anotherDog);
}
public Dog getSelf() {
return new DomesticDog();
}
public String toString() {
return super.toString() + " named " + name;
}
}
public class Couple<T extends SelfMaker<? super T>> {
protected T first;
protected T second;
public String toString() {
return first.toString() + " and " + second.toString();
}
}
如果你不想在Dog的每个子类中覆盖getSelf(),你可以在类Dog中进行以下更改:
public Dog getSelf() {
Class<? extends Dog> thisClass = this.getClass();
try {
return thisClass.newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
throw new UnsupportedOperationException(thisClass
+ " does not supply a public no-arg constructor");
}
这可以保证getSelf()返回的每个值都是this.getClass()
的实例。但是你仍然需要为子类强制转换procreate()的返回值。无法将返回类型明确指定为this.getClass()
。
答案 1 :(得分:0)
您必须使Dog
通用和抽象,并使用指示getSelf()
结果的类型参数。然后,每种类型的Dog
都需要将它们作为参数实现:
public abstract class Dog<T> implements SelfMaker<T> {
String color;
public String toString() {
return "some " + color + " dog";
}
public T procreate(T anotherDog) {
T son = getSelf();
son.color = color;
return son;
}
}
public class DomesticDog extends Dog<DomesticDog> {
private String name;
public String toString() {
return super.toString() + " named " + name;
}
@Override
public DomesticDog getSelf() {
return new DomesticDog();
}
}