嵌套类C#

时间:2016-09-30 20:33:20

标签: c# inheritance nested

美丽代码的粉丝。

我想通过两种方式提出我的问题。理解我可能会有用。

1)有2个类的代码。其中一个是嵌套的。嵌套类用于访问其他字段的私有字段。我想继承类B:一个{类BUnit:AUnit {}}具有相同的功能,但在B和BUnits类中有更多的方法和字段。怎么做?

class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        a.Add();
        a.Add();
        a.Add();
        bool res=a[0].Rename("1");//res=true;
        res = a[1].Rename("1");//res= false;
        Console.ReadKey();
    }
}
class A
{
    private List<AUnit> AUnits;
    public AUnit this[int index] {get {return AUnits[index];}}

    public A()//ctor
    {
        AUnits = new List<AUnit>();
    }
    public void Add()
    {
        this.AUnits.Add(new AUnit(this));
    }

    public class AUnit
    {
        private string NamePr;
        private A Container;
        public AUnit(A container)//ctor 
        {
            NamePr = "Default";
            this.Container = container;
        }
        public string Name { get { return this.NamePr; } }
        public Boolean Rename(String newName)
        {
            Boolean res = true;
            foreach (AUnit unt in this.Container.AUnits)
            {
                if (unt.Name == newName) res = false;
            }
            if (res) this.NamePr = String.Copy(newName);
            return res;
        }
    }
}

2)有两个非常相似的“事物” - A类和B类。是否有可能将它们的共同部分分开,然后从它“继承”这两个“事物”?例如,我想添加一些方法,如GetUnitsCount()或RemoveUnit(),这两种方法都很常见。所以我应该把这个方法“CopyPaste”给A和B,但这不是个好主意。最好在一个地方改变他们的共同部分。没有什么重要的可以做 - 继承或接口或其他任何东西。重要 - 如何?

 class Program
 {
    static void Main(string[] args)
    {
        A a = new A();
        a.Add();
        a[0].objB.Add();
        a[0].objB.Add();
        a[0].objB[0].Val1 = 1;

        int res = a[0].objB[0].Val1 + a[0].objB[0].Val2;

        Console.ReadKey();
    }
}
class A
{
    private List<AUnit> Units;
    public AUnit this[int index] {get {return Units[index];}}

    public A()//ctor
    {
        Units = new List<AUnit>();
    }
    public void Add()
    {
        this.Units.Add(new AUnit(this));
    }

    public class AUnit
    {
        private string NamePr;
        private A Container;
        public B objB;
        public AUnit(A container)//ctor 
        {
            NamePr = "Default";
            this.Container = container;
            this.objB = new B();
        }
        public string Name { get { return this.NamePr; } }
        public Boolean Rename(String newName)
        {
            Boolean res = true;
            foreach (AUnit unt in this.Container.Units)
            {
                if (unt.Name == newName) res = false;
            }
            if (res) this.NamePr = String.Copy(newName);
            return res;
        }
    }
}


class B
{
    private List<BUnit> Units;
    public BUnit this[int index] { get { return Units[index]; } }

    public B()//ctor
    {
        Units = new List<BUnit>();
    }
    public void Add()
    {
        this.Units.Add(new BUnit(this));
    }

    public class BUnit
    {
        private string NamePr;
        private B Container;
        public int Val1{get;set;}
        public int Val2{get;set;}
        public BUnit(B container)//ctor 
        {
            NamePr = "Default";
            this.Container = container;
            this.Val1 = 10;
            this.Val2 = 17;
        }
        public string Name { get { return this.NamePr; } }
        public Boolean Rename(String newName)
        {
            Boolean res = true;
            foreach (BUnit unt in this.Container.Units)
            {
                if (unt.Name == newName) res = false;
            }
            if (res) this.NamePr = String.Copy(newName);
            return res;
        }
    }
}

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

要回答您的第一个问题,您需要BUnitAUnit继承的唯一内容是限定 AUnit

public class BUnit : A.AUnit
{
    ....
}

从那里我相信你的问题是基本的继承,它对于嵌套类没有区别。嵌套类纯粹是为了组织 - 当你继承“包含”类时,它们不会被继承。