zoukankan      html  css  js  c++  java
  • 集合类中嵌套定义和引用的举例

            在开发应用程序时,有时候需要定义简单的类,有时候需要定义复杂的集合类型,复杂的集合类型既可以用于存储简单类的对象,也可以用于存储其它集合类型。这时候就用到了集合类中嵌套定义和引用。下面代码示例。

    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");
            }
        }
    }

    详细的解析就不多说了,大家运行源码体会吧。

    源码下载:源码

  • 相关阅读:
    上传图片,将图片保存在腾讯云(2种方式)
    由ping所引发的思考~
    php面试上机题(2018-3-3)
    【八】jqeury之click事件[添加及删除数据]
    【七】jquery之属性attr、 removeAttr、prop[全选全不选及反选]
    【六】jquery之HTML代码/文本/值[下拉列表框、多选框、单选框的选中]
    【五】jquery之事件(focus事件与blur事件)[提示语的出现及消失时机]
    小白懂算法之基数排序
    mysql_sql199语法介绍
    Python基本编程快速入门
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/11852363.html
Copyright © 2011-2022 走看看