简体   繁体   中英

JAVA: how to pass an object within one object to another object?

I need to pass an object (for example object from Class DATA) which is within another object (for example object from Class NODE) to another object (from class NODE)? The Node objects are nodes of a tree. The tree implementation is :

public class Tree {

private Node rootElement;

public Tree() {
    super();
}

public Node getRootElement() {
    return this.rootElement;
}

public void setRootElement(Node rootElement) {
    this.rootElement = rootElement;
}

and then for Node I have:

public class Node {

public MyNode data;
public List<Node> children;
public int  childNo;  

public Node() {
    super();
}

public Node(MyNode data) {
    this();
    setData(data);
}

 public Node(MyNode data, int childNo) {
    this();
    setData(data);
    this.childNo=childNo;
}

public void setChildren(List<Node> children) {
    this.children = children;
}

public void addChild(Node child) {
    if (children == null) {
        children = new ArrayList<Node>();
    }
    children.add(child);
}

public MyNode getData() {
    return this.data;
}

public void setData(MyNode data) {
    this.data = data;
}

and for Mynode I have :

public class MyNode {

public String ID = new String();   //UUID
protected String parentID;
MyNodesDATA d = new MyNodesDATA();


public MyNode() {
    setID(UUID.randomUUID().toString());
}

public MyNodesDATA getMyNodesDATA () {
    return this.MyNodesDATA ;
}

public void setData(MyNodesDATA d) {
    this.MyNodesDATA = data;
}

and this is MyNodesDATA

public class MyNodesDATA {
int iD;
String name;
}

So each node of the tree has an object of MyNode Type and each MyNode Object has a data as an object from MyNodesDATA class. I have already initialised the tree with a predefined structures of hierarchical nodes using some recursive methods which I didn't put here ... Now, nodes of the tree in lover levels process their data and need to send them to their parents (USING THEIR PARENT ID). I stuck here... how a node (an object) can send it's data to another node (another object) (let say its parent in this scenario) using only one ID variable of the other object ... in a simpler way I need a method to get (ID and DATA) and send the data to the object which has the ID !! and of course there should be another function or event handler in receiver side to take action once receives a Data ....

First of all, you probably want to make the DATA instance created in the Node constructor an instance member. Otherwise it can be released by the garbage collector once the constructor is done.

Then can either pass the DATA parameter to a constructor of Node , or you can have a setter method that would accept a DATA parameter.

public class Node {
    int iD;
    private DATA d;

    public Node(){
        this.d = new DATA(); // or this(new DATA());
    }

    public Node(DATA d){
        this.d = d;
    }

    public void setData (DATA d) {
        this.d = d;
    }

    public DATA getData() {
        return this.d;
    }
}

Now you have several options for 2 Node instances to share the DATA member :

public class MAIN {
    public static void main(String[] args) {
        DATA data = new DATA();
        Node node1 = new Node(data);
        Node node2 = new Node(data);
    }
}

or

public class MAIN {
    public static void main(String[] args) {
        Node node1 = new Node();
        Node node2 = new Node(node1.getData());
    }
}

or

public class MAIN {
    public static void main(String[] args) {
        Node node1 = new Node();
        Node node2 = new Node(null);
        node2.setData (node1.getData());
    }
}

In your example, Node does not have an object of DATA within it; you've simply initialised one in the constructor then allowed it to go out of scope. You also need to modify your constructor if you want to pass in a DATA object, so change Node to be:

public class Node {
    int iD;
    DATA d;
    public Node(){
        d = new DATA();
    }
    public Node(DATA d){
        this.d = d;
    }
}

so that you have the DATA instance stored. Then simply provide a getter, eg:

public DATA getData() {
    return d;
}

and then you can do:

public class MAIN {
    public static void main(String[] args) {
        Node node1 = new Node();
        Node node2 = new Node(node1.getData());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM