这个问题是在C++环境中提出的,但我对Java很好奇。对虚拟方法的关注并不适用(我认为),但是如果您遇到这种情况:
abstract class Pet
{
private String name;
public Pet setName(String name) { this.name = name; return this; }
}
class Cat extends Pet
{
public Cat catchMice() {
System.out.println("I caught a mouse!");
return this;
}
}
class Dog extends Pet
{
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return this;
}
}
class Bird extends Pet
{
public Bird layEgg() {
...
return this;
}
}
{
Cat c = new Cat();
c.setName("Morris").catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee(); // error! setName returns Pet, not Dog
Bird b = new Bird();
b.setName("Tweety").layEgg(); // error! setName returns Pet, not Bird
}
在这种类层次结构中,是否有任何方法可以返回this,而不(有效地)向上转换对象类型?
现在我明白了为什么这么多人讨厌Java。
如果希望避免编译器发出未选中的强制转换警告(并且不希望@suppresswarnings("unchecked"),则需要执行以下操作:
首先,您对宠物的定义必须是自引用的,因为宠物总是一个通用类型:
abstract class Pet >
其次,setname中的(T) this强制转换也未选中。要避免这种情况,请使用安吉丽卡·兰格的《优秀仿制药常见问题解答》中的"getthis"技术:
The"getThis" trick provides a way to
recover the exact type of the this
reference.
这将导致下面的代码编译并运行,而不发出警告。如果您想扩展子类,那么该技术仍然有效(尽管您可能需要将中间类泛型化)。
产生的代码是:
public class TestClass {
static abstract class Pet > {
private String name;
protected abstract T getThis();
public T setName(String name) {
this.name = name;
return getThis(); }
}
static class Cat extends Pet {
@Override protected Cat getThis() { return this; }
public Cat catchMice() {
System.out.println("I caught a mouse!");
return getThis();
}
}
static class Dog extends Pet {
@Override protected Dog getThis() { return this; }
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return getThis();
}
}
public static void main(String[] args) {
Cat c = new Cat();
c.setName("Morris").catchMice();
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee();
}
}
代码通过这种方式变得更干净,我将花一些时间阅读安吉丽卡的完整文章,THXVM!
class Snake extends Pet{@Override protected Cat getThis() {return new Cat();}}
不过,当您有中间的非抽象、非最终类并且需要创建实例时,这会变得有点棘手。例如,假设你有一个static class Poodle extends Dog,把Dog改为static class Dog> extends Pet。现在创建一个原始的Dog实例是很困难的。
有没有什么方法可以和annonymous类一起使用?我找不到一种方法让它自己引用泛型t:。/
我创造了一个有用的东西:class PetAnnon extends Pet{},在每一个Annonymous类上,我都像new Pet{...一样使用它,现在像T get(Classcl){return (T)this.val;}这样的方法将再次有效。
这个老把戏怎么样:
abstract class Pet
{
private String name;
public T setName(String name) { this.name = name; return (T) this; }
}
class Cat extends Pet
{
/* ... */
}
class Dog extends Pet
{
/* ... */
}
+1.表达得比我简洁。但是考虑到Java泛型已经存在了多久,它会有多大的技巧呢?
啊哈,我想会有一些仿制药,只是不知道是什么。谢谢!
Steve B:在Java中它不是旧的(实际上,我不认为它已经在Java中使用过),但是它已经在C++中使用了很长时间。
嗯,你能再加一个上抛和下抛的例子吗?例如PET?>pet=c;((猫)pet).catchmices();(我有这个权利吗?)
stackoverflow.com/questions/149336/…stackoverflow.com/questions/9138027/…
如果你有一个迭代器,那么这样做:public Iteratoriterator() {,这样你就不必为猫和狗实现一个迭代器,但是你仍然可以循环猫和狗,实际上得到一个猫或狗而不是宠物!
不,不是真的。您可以通过使用协变返回类型来解决这个问题(感谢McDowell提供正确的名称):
@Override
public Cat setName(String name) {
super.setName(name);
return this;
}
(协变返回类型仅在Java 5和以上,如果这是您的关注点)。
这有点复杂,但是你可以用泛型来实现:
abstract class Pet< T extends Pet > {
private String name;
public T setName( String name ) {
this.name = name;
return (T)this;
}
public static class Cat extends Pet< Cat > {
public Cat catchMice() {
System.out.println("I caught a mouse!" );
return this;
}
}
public static class Dog extends Pet< Dog > {
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!" );
return this;
}
}
public static void main (String[] args){
Cat c = new Cat();
c.setName("Morris" ).catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName("Snoopy" ).catchFrisbee(); // error! setName returns Pet, not Dog
}
}
public class Pet {
private String name;
public AnimalType setName(String name) {
this.name = name; return (AnimalType)this;
}
}
和
public class Cat extends Pet {
public Cat catchMice() {return this;}
public static void main(String[] args) {
Cat c = new Cat().setName("bob").catchMice();
}
}
@史蒂夫B.-+1,跟我打赌!
编辑:复制/粘贴代码就成功了。我意识到我的基础班是X extends Y,而不是X extends Y。解决了!