Java generics to enforce return type of abstract method

前端 未结 5 1964
既然无缘
既然无缘 2020-12-17 18:59

I have the following situation :

abstract class X { abstract X someMethod (...) {...} }.

Now I want to constrain any implementation of X to

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 19:22

    This works as well;

    abstract class X {
        public abstract T yourMethod();
    }
    class X1 extends X {
        public X1 yourMethod() {
            return this;
        }
    }
    class X2 extends X {
        public X2 yourMethod() {
            return this;
        }
    }
    

提交回复
热议问题