简体   繁体   English

我如何设计课程?

[英]How do I design my class?

I am trying to figure out how I should arrange my classes so that I can achieve a following. 我想弄清楚我应该如何安排我的课程,以便我可以实现以下目标。

So, ultimately I have a graph which takes nodes and edges. 所以,最终我有一个图表,它采用节点和边。

And the idea is to run some graph algorithm based on these nodes and edges. 并且想法是基于这些节点和边缘运行一些图算法。

So, the basic unit Graph class takes are 所以,Graph类的基本单位是

private Node node
private Edge Edge

and you form a graph .. right 你形成一个图表..对

Now, here is the issue. 现在,这是问题所在。

I have a special Edge say special Edge which implements the earlier Edge and has some more special properties of its own. 我有一个特殊的Edge说特殊的Edge,它实现了早期的Edge,并且有一些更特殊的属性。

So I have a graph of type Node, SpecialEdge 所以我有Node, SpecialEdge类型的图表

Now, how do I use graph G which accepts Edge and not SpecialEdge?? 现在,我如何使用接受Edge而不是SpecialEdge的图G?

Any suggestions would be appreciated. 任何建议,将不胜感激。 Thanks 谢谢

You should create two different subclasses of Edge, namely BasicEdge and SpecialEdge. 您应该创建两个不同的Edge子类,即BasicEdge和SpecialEdge。

A graph that should accept both should be Edge. 应该接受两者的图表应该是Edge。 If you only want one type, accept either BasicEdge or SpecialEdge. 如果您只想要一种类型,请接受BasicEdge或SpecialEdge。

You can define an interface Edge where subclasses like SpecialEdge would implement it. 您可以定义一个接口Edge,其中像SpecialEdge这样的子类将实现它。 You may have many implementations of Edge and it will always work. 您可能有许多Edge的实现,它将始终有效。

Also, since your Graph contains many nodes and many edges, you may consider using List<Node> and List<Edge> in your design. 此外,由于您的Graph包含许多节点和许多边,因此您可以考虑在设计中使用List<Node>List<Edge>

Edit: According to Nick, you should use Set<Node> and Set<Edge> since the order is not an issue in this case. 编辑:根据Nick的说法,您应该使用Set<Node>Set<Edge>因为在这种情况下订单不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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