mercredi 20 mars 2019

Extends class that has itself as a member

Suppose I have to use a lib that has a class that implements a binary tree, with signature like that:

class Node {
    public void setN0(Node n);
    public void setN1(Node n);
    public Node getN0();
    public Node getN1();
}

So, I want to create a class that extends this, to put a graphical layer over it, like

class UINode extends Node {
    private int coordinateX;
    private int coordinateY;
}

But I can't do that because its not possible to pass UInode's to be Node childs, like

UINode root = new UINode();
UINode n0 = new UINode();
UINode n1 = new UINode();
root.setN0(n0);  //Not possible
root.setN1(n1);  //Not possible

Is there a design pattern or something to deal with this situation? Or there is a simple solution that I cant see?

Aucun commentaire:

Enregistrer un commentaire