在开发应用程序时,有时候需要定义简单的类,有时候需要定义复杂的集合类型,复杂的集合类型既可以用于存储简单类的对象,也可以用于存储其它集合类型。这时候就用到了集合类中嵌套定义和引用。下面代码示例。
ListBase类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
public class ListBase
{
//int number;
public MyList parentList;
}
}
MyList类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ListEmbeddedTest
{
public class MyList : ListBase
{
public ArrayList m_children = new ArrayList();
}
}
World类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
public class World : ListBase
{
public double radius;
public string name;
}
}
PathPoint类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
public class PathPoint : ListBase
{
public int pointCount;
}
}
PathList类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
public class PathList : MyList
{
public int totalNum;
}
}
Consumer类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
//消费者:使用项目中定义的所有类
public class Consumer
{
public string consumerName;
public MyList renderableObjectsList=new MyList();
}
}
Program类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListEmbeddedTest
{
class Program
{
static void Main(string[] args)
{
PathPoint pathPoint=new PathPoint();
pathPoint.pointCount=4;
PathPoint pathPoint1 = new PathPoint();
pathPoint1.pointCount = 3;
PathList pathList = new PathList();
pathList.totalNum = 7;
pathList.m_children.Add(pathPoint);
pathList.m_children.Add(pathPoint1);
pathPoint.parentList = pathList;
pathPoint1.parentList = pathList;
World world = new World();
world.radius = 6378137;
world.name = "Earth";
Consumer consumer = new Consumer();
consumer.consumerName = "主应用程序";
consumer.renderableObjectsList.m_children.Add(pathList);
consumer.renderableObjectsList.m_children.Add(world);
System.Console.WriteLine("Hello");
}
}
}
详细的解析就不多说了,大家运行源码体会吧。
源码下载:源码