Tag: 自引用

如何引用接口在Java中实现的类类型?

我遇到了我正在制作的程序中的接口问题。 我想创建一个接口,它的一个方法接收/返回对自己对象类型的引用。 它是这样的: public interface I { ? getSelf(); } public class A implements I { A getSelf() { return this; } } public class B implements I { B getSelf() { return this; } } 我不能使用“I”,它是“?”,因为我不想返回对接口的引用,而是类。 我搜索并发现Java中没有办法“自我引用”,所以我不能只用“?”来代替它。 在“自我”关键字或类似的示例中。 实际上,我想出了一个类似的解决方案 public interface I { SELF getSelf(); } public class A implements I { A getSelf() […]