基本上我正在尝试实现这样的事情,其中合作伙伴节点为“ type1”,客户端节点为“ type2”,用户节点为“ type3”。每个节点可以具有多个子节点。因此,Partner1下可以有任意数量的客户端节点,类似地,客户端节点下可以有任意数量的用户。
我已经开始实现,但是现在被卡住了。我编写的代码如下。
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<Enum,List<Enum>> clientproperty = new HashMap<Enum,List<Enum>>();
public Clients(String client_name, String client_id, int client_node_id,Map<Enum,List<Enum>> 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;
}
}
}
让我知道是否需要一些见解
首先是一些更具体的事情:
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
换句话说:您在类中投入了太多的“角色”。
含义:您想引入各种抽象。例如:
interface GraphNode<N, C extends GraphNode> {
N getNodeContent();
List<C> getChildrenNodes();
}
现在您可以表达:任何“节点”确实具有某些内容(可以是用户或客户端或任何对象);它具有一个(或一组)子代(也称为“节点”)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句