日期:2014-05-17  浏览次数:20880 次

请教一个自定义数据绑定问题,关于同一个类不同属性怎么设计
C# code
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            List<SempleList> AddLyr = new List<SempleList>();
            AddLyr.Add(new SempleList(0));
            AddLyr.Add(new SempleList(1,false));
        }
        public class SempleList
        {
            int ID;
            bool SameName;
            public SempleList(int ID)
            {
                this.ID = ID;
            }

            public SempleList(int ID, bool SameName)
                : this(ID)
            {
                this.SameName = SameName;
            }
        }

代码如上,怎么设计类封装得到AddLyr[0]没有“SameName”属性,而AddLyr[1]有“SameName”属性?

------解决方案--------------------
定义一个接口,派生两个子类。
------解决方案--------------------
做成两个类吧,AddLyr[1]里面加父类,AddLyr[0]里面加子类,子类里面比父类里面多一个同名的属性
------解决方案--------------------
C# code

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            List<SempleListBase> AddLyr = new List<SempleList>();
            AddLyr.Add(new SempleList1(0));
            AddLyr.Add(new SempleList2(1,false));
        }
        public class SempleList1 :SempleListBase
        {
            int ID;
            ...
        }
        public class SempleList2 :SempleListBase
        {
            int ID;
            bool SameName;
        }