个人做哪方面的网站,品牌网站建设重點大蝌蚪,新织梦官网,北京app建设 网站开发公司这个问题是在C环境中提出的#xff0c;但我对Java很好奇。对虚拟方法的关注并不适用(我认为)#xff0c;但是如果您遇到这种情况#xff1a;abstract class Pet{private String name;public Pet setName(String name) { this.name name; return this; }}class Cat extends …这个问题是在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 CatDog d new Dog();d.setName(Snoopy).catchFrisbee(); // error! setName returns Pet, not DogBird b new Bird();b.setName(Tweety).layEgg(); // error! setName returns Pet, not Bird}在这种类层次结构中是否有任何方法可以返回this而不(有效地)向上转换对象类型现在我明白了为什么这么多人讨厌Java。如果希望避免编译器发出未选中的强制转换警告(并且不希望suppresswarnings(unchecked)则需要执行以下操作首先您对宠物的定义必须是自引用的因为宠物总是一个通用类型abstract class Pet 其次setname中的(T) this强制转换也未选中。要避免这种情况请使用安吉丽卡·兰格的《优秀仿制药常见问题解答》中的getthis技术ThegetThis trick provides a way torecover the exact type of the thisreference.这将导致下面的代码编译并运行而不发出警告。如果您想扩展子类那么该技术仍然有效(尽管您可能需要将中间类泛型化)。产生的代码是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();}}代码通过这种方式变得更干净我将花一些时间阅读安吉丽卡的完整文章THXVMclass 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中使用了很长时间。嗯你能再加一个上抛和下抛的例子吗例如PETpetc((猫)pet).catchmices()(我有这个权利吗)stackoverflow.com/questions/149336/hellipstackoverflow.com/questions/9138027/hellip如果你有一个迭代器那么这样做public Iteratoriterator() {这样你就不必为猫和狗实现一个迭代器但是你仍然可以循环猫和狗实际上得到一个猫或狗而不是宠物不不是真的。您可以通过使用协变返回类型来解决这个问题(感谢McDowell提供正确的名称)Overridepublic 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 CatDog 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。解决了