Javagenerics类型,可以扩展,也可以是父类
我正在寻找一些如下所示的代码:
public class Parent<T is_or_extends Parent> { public T function() { // do some stuff // return correct(); } public T correct() { return (T) this; } }
这样任何子类都可以访问其他父函数,但仍然是独立的类(没有将function()的返回值向上转换为Parent的实例)。 程序员也可以独立使用Parent类(因此可以创建Parent的实例而不使用function()向下转换返回对象)
执行:
public class Child extends Parent {}
用法:
Child child = new Child(); // ok Parent polymorph = new Child(); // ok Parent parent = new Parent(); // ERROR!
为了概述差异,这不是方法链的问题。 相反,当父类接受子类的generics时,它不能再用作它自己的类(使用上面的代码),因为new Parent()
将不会编译(当替换“ is_or_extends
” is_or_extends
“with” extends
“)
我的目标:
我想要实现的是一个父类,当扩展时, function()
将返回子类的对象而不是父类。
我会使用generics类型来告诉父类哪个子类调用了该函数,但是我不能再使用父类的对象而不会遇到exception。 一个例子如下:
public static class Parent<T extends Parent> {} public static class Child extends Parent {} public static void main(String[] args) { // This will not compile as Parent cannot extend Parent Parent parent = new Parent(); // This will work as Child extends Parent Child child = new Child(); }
提前致谢。
extends
表示“是或扩展”。 您的示例无法编译的原因是因为内部Parent
类型是原始类型,它不是T extends Parent
的有效替代。 “正确”类型如下所示: Parent
ad infinitum。 显然这种类型是不可能宣布的。
一种解决方案是将其实例化为:
Parent> parent = new Parent<>(); Parent> derived = parent.function();
这是有效的,因为编译器已经知道T
是Parent>
一些子类。 这个技巧的一个缺点是,如果无法推断类型,它将无法工作,例如使用匿名类时。
另一种可能的方法 – 取决于父/子关系的性质 – 是创建一个额外的类来扩展基础,只是为了它的类型解析:
// Parent functionality in here public static abstract class Base> {} // This can be empty, or have just constructors if necessary public static class Simple extends Base {} // Child functionality in here public static class Extended extends Base {}