[英]OOP design and Lists and collections (C#)
我需要设计一个简单的软件,我想知道最好的软件。
首先,对新手问题感到抱歉:使用列表的最佳方法是什么? 我尝试了几次,却没有意识到实例化它们的正确方法以及如何在OOP中使用它们。 谁能帮我清理一下?
第二个问题是:我有以下类(格式宽松,而不是代码)
Tenement:
tenementID Int32,
polygons Polygon, (here i need to use a list, but how?)
status TenementStatus;
Polygon:
polygonID Int32,
vertexes vertex (need to use a list here too),
areaPolygon;
Vertex:
vertexID Int32,
X Int32,
Y Int32;
我对这些课程的想法正确吗? 有没有更简单的方法可以做到这一点?
在C#中,您将要使用List<T>
类来处理作为列表的集合。
例如:
public class Tenement
{
public Tenement()
{
this.Polygons = new List<Polygon>(); // Create the list here...
}
public int Id { get; set; }
public IList<Polygon> Polygons { get; private set; }
// ...
编辑:如果需要控制如何添加或删除项目,例如检查以确保多边形有效,则还可以执行以下操作:
public class Tenement
{
public Tenement()
{
this.polygons = new List<Polygon>(); // Create the list here...
}
private List<Polygon> polygons;
public int Id { get; set; }
public IEnumerable<Polygon> Polygons {
get { return this.polygons; }
}
public void AddPolygon(Polygon polygon) {
// Do extra stuff here, then:
this.polygons.Add(polygon);
}
// ...
在C#中,一个List
只是一个数据结构,它表示可以通过索引访问的对象的集合。 如果这句话有意义,那么您将了解列表所需的全部知识(至少对于此示例而言)。 如果没有,您可以先查看List(T)
类的MSDN文档。
例:
// Declare a new list of vertices
List<Vertex> vertices = new List<Vertex>();
// Add a couple of vertices
vertices.Add(new Vertex(0, 0, 0));
vertices.Add(new Vertex(1, 100, 100));
vertices.Add(new Vertex(2, 50, 50));
// Get a vertex back out of the list by using its index
Vertex temp = vertices[1]; // this is the second vertex we added above
如果您正在寻找一个简单的List对象...
List<Vertex> vertices = new List<Vertex>();
和
List<TenementStatus> statuses = new List<TenementStatus>();
这就是您需要的简单声明...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.