[英]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)? 我需要将另一个对象(例如,来自NODE类的对象)内的对象(例如,来自DATA类的对象)传递给另一个对象(来自NODE类)? The Node objects are nodes of a tree. Node对象是树的节点。 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: 然后对于Node我有:
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 : 对于Mynode我有:
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 这是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. 因此,树的每个节点都有一个MyNode类型的对象,每个MyNode对象都有一个数据作为MyNodesDATA类中的对象。 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). 我已经使用一些递归方法(没有在这里使用递归方法)使用预定义的层次结构来初始化树...现在,恋人级别的树节点处理其数据并需要将其发送给父母(使用它们家长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 !! 我困在这里...一个节点(一个对象)如何仅使用另一个对象的一个ID变量将其数据发送到另一个节点(另一个对象)(在这种情况下,假设其父对象)...需要一种获取(ID和DATA)并将数据发送到具有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. 首先,您可能希望使在Node
构造函数中创建的DATA
实例成为实例成员。 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. 然后,可以将DATA
参数传递给Node
的构造函数,也可以有一个将接受DATA
参数的setter方法。
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 : 现在,您可以使用两个Node
实例的多个选项来共享DATA
成员:
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; 在您的示例中, Node
中没有 DATA
对象。 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: 如果要传递DATA
对象,还需要修改构造函数,因此将Node
更改为:
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. 这样您就可以存储DATA
实例。 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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.