How to get compile time reference for current class instance

I have a member of a class that is a type of my class, like a linked list:

class MyLinkedList {
    ...
    public MyLinkedList next;
    ...
}

When I extend this class

class MyExtendedLinkedList extends MyLinkedList{
    ...
    public doStuff(){
    ...
    }
}

I can not do something like

MyExtendedLinkedList myExLL = ...//Get some somewhere
myExLL.next.doStuff();

Since it nextdoes not return an instance MyExtendedLinkedList. Obviously this can be done with type casting, but is there a compile-time type safe declaration method nextso that I can get a derived type for all my derived classes?

+4
source share
1 answer
class MyLinkedList<T extends MyLinkedList<T>> {
  public T next;
}

class MyExtendedLinkedList extends MyLinkedList<MyExtendedLinkedList> {
  ...
}

, MyExtendedLinkedList ; , MyLinkedList , .

+3

Source: https://habr.com/ru/post/1676510/


All Articles