Ben*_*laa 12 java oop generics
这有点难以解释,但我到处都看,我找不到任何好的答案.
我也看过Stack Overflow问题如何引用接口在Java中实现的类类型?和如何返回相同类型的对象的实例作为类中使用Java 6过去了?,但他们无法回答我的问题.应用继承时有一个例外.
有一个例子,让它更容易理解:
假设我有一些名为SelfMaker的界面:
public interface SelfMaker <SELF>{
public SELF getSelf();
}
Run Code Online (Sandbox Code Playgroud)
A有一只狗,可以与另一只狗生育.所以狗是一个"SelfMaker",像这样:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我有一只国内狗,他是一只狗,但它有一个可爱的家庭,他给他起了个名字.像这样:
public class DomesticDog extends Dog {
private String name;
public String toString() {
return super.toString() + " named " + name;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一些课程处理"SelfMaker"的事情,让我们把这个课称为"情侣".像这样:
public class Couple<T extends SelfMaker<T>> {
private T first;
private T second;
public String toString() {
return first.toString() + " and " + second.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
除外:
当我想创建几个DomesticDogs 时会出现异常.像这样:
public class CoupleOfDomesticDogs extends Couple<DomesticDog>{
public DomesticDog procreate(){
DomesticDog son = first.procreate(second);
return son;
}
}
Run Code Online (Sandbox Code Playgroud)
这将引发<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<T extends SelfMaker<?>>但是"son"不会是DomesticDog(我希望"son"成为DomesticDog).如果我添加一些演员,那么它将编译,但它将不太清晰.
所以...这里有一个问题:有没有办法在没有铸造和概括的情况下实现这一目标?
没有铸造,我无法想到这样做.如果覆盖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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果你不想在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");
}
Run Code Online (Sandbox Code Playgroud)
这保证了getSelf()返回的每个值都是this.getClass().但是你仍然需要为子类强制转换procreate()的返回值.无法将返回类型明确指定为this.getClass().