这有点难以解释,但我到处都找遍了,找不到任何好的答案。
我还看到了 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 有一只狗,它可以与另一只狗繁殖。所以狗是一个“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();
}
}
但是,我有一只 DomesticDog,它是一只狗,但它有一个可爱的家庭给他取名。像这样:
public class DomesticDog extends Dog {
private String name;
public String toString() {
return super.toString() + " named " + name;
}
}
现在,我有一些类可以处理一些“SelfMaker”的事情,我们称这个类为“Couple”。像这样:
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<?>>
但“儿子”不会是 DomesticDog(我希望“儿子”成为 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();
}
}
如果您不想在 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()
。
关于java - 如何引用当前类的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037572/