Java – use list > Member definition Abstract generic class
Even trying to propose a clean title is a challenge
The basic idea is to define two superclasses: one for "child" items, whose members refer to their "parent", and the other for the "parent" list containing child objects The links from child - > parent and parent - > child are symmetrical Each parent / child superclass has subclasses for defining and implementing other functions There is always a parallel subclass so that the child pair is the same as the parent pair That is, the parent contains only child references, while the child references only the parent - there is no "crossover" between subtypes
How can I represent this? I've been doing this for days, and the more creative I am with multilevel nested generic types, the worse it gets That's what I want to do:
abstract class ChildBase<T extends ChildBase<T>> { ParentBase<T> parent; } abstract class ParentBase<T extends ChildBase<T>> { LinkedList<T> childList; } class ChildSub extends ChildBase<ChildSub> { // ... class specific stuff } class ParentSub extends ParentBase<ChildSub> { // ... class specific stuff }
This is a mess I suspect there is a simpler and more direct way, which may be a completely different direction
Solution
Do you mean that the parentsub class extends parentbase < childsub >?
If you make this change, it will compile
In fact, it's not that bad compared with some other methods I've seen used in generic drugs There is no completely different way to achieve this goal
The only option (I think you should seriously consider) is to make childbase and parentbase non generic and use casts when necessary Generics were introduced into the language to help programmers write correct, type - safe programs When there are complex or circular relationships between your classes, figuring out how to use generics correctly becomes so difficult and time-consuming that the benefits of using them do not exist at all