Is there a way to refer to the current type with a type variable?

后端 未结 7 1675
悲&欢浪女
悲&欢浪女 2020-11-22 03:01

Suppose I\'m trying to write a function to return an instance of the current type. Is there a way to make T refer to the exact subtype (so T should

7条回答
  •  清酒与你
    2020-11-22 03:43

    Just write:

    class A {
        A foo() { ... }
    }
    
    class B extends A {
        @Override
        B foo() { ... }
    }
    

    assuming you're using Java 1.5+ (covariant return types).

提交回复
热议问题