java 孩子节点所有,如何使树具有多种类型的节点,并且每个节点在Java中可以具有多个子节点...

Basically i am trying to implement something like this, where partner node is of say "type1", client node is of "type2" and user nodes is of "type3". And each of the nodes can have multiple number of child nodes. So Partner1 can have any number of client nodes under it, similarly client nodes can have any number of users under it.

5VTEW.png

I have started the implementation but i am stuck now.The code which i have written is as follows.

public class ClientProperty {

public class Root{} //NodeType1

public class Partner{ //NodeType2

public String partner_id;

public String partner_name;

public int partner_node_id;

public Partner(String partner_id,String partner_name,int partner_node_id){

this.partner_id = partner_id;

this.partner_name = partner_name;

this.partner_node_id = partner_node_id;

}

}

public class Clients{ //NodeType3

public String client_name;

public String client_id;

public int client_node_id;

public Map> clientproperty = new HashMap>();

public Clients(String client_name, String client_id, int client_node_id,Map> clientproperty){

this.client_name = client_name;

this.client_id = client_id;

this.client_node_id = client_node_id;

this.clientproperty = clientproperty;

}

}

public class Users{ //NodeType4

public String user_name;

public String user_id;

public int user_node_id;

public Users(String user_id,String user_name, int user_node_id){

this.user_id = user_id;

this.user_name = user_name;

this.user_node_id = user_node_id;

}

}

public class Node{

Node next;

Object nodes;

public Node(){

next = null;

}

public Node(Object nodes, Node next){

this.nodes = nodes;

this.next = next;

}

}

}

Let me know if some insights is required

解决方案

First some more unspecific things:

You want to read about data encapsulation. Putting all public fields on your classes is simply wrong. You actually want to hide such information as far as possible.

Then you want to read about java coding style conventions; as you are violating quite some of them (which simply doesn't help when you show your code to more experienced java coders).

Finally, most important: you want to read quite a bit about OO design in general (I recommend "Agile practices" by Robert Martin; there is a free PDF of the "C# version" of that book):

It starts wit the fact that

a) being a client/user is a "different responsibility" than

b) being some element in a graph

In other words: you are putting way too many "roles" into your classes.

Meaning: you want to introduce various kinds of abstractions. For example:

interface GraphNode {

N getNodeContent();

List getChildrenNodes();

}

Now you can express: any "node" does have some content (which could be a User or Client or whatever object); and it has a list (or set) of children, that are also "nodes".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值