继承自基类C#

时间:2017-02-07 12:20:02

标签: c#

不确定我做错了什么,但不是直接从基类继承的属性。如果有:

public class Fruit{
   string[] GroupFruit{get;set;}

   public Fruit()
   {
   }
}

public class Citrus : Fruit{
    GroupFruit = new string[]{"lemon", "lime", "orange"}

   public Citrus()
   {
   }
}

我似乎无法在Citrus课程中设置GroupFruit。 谢谢。

杰罗姆

1 个答案:

答案 0 :(得分:2)

您需要在子类的构造函数中初始化它,并且它必须至少为protected

public class Fruit
{
    protected string[] GroupFruit { get; set; }

    public Fruit()
    {
    }
}

public class Citrus : Fruit
{

    public Citrus()
    {
        base.GroupFruit = new string[] {"lemon", "lime", "orange"};
    }
}