简体   繁体   English

从基类 C# 继承

[英]Inheriting from Base Class C#

Not sure what i'm doing wrong but are Properties not directly inherited from a base class.不确定我做错了什么,但属性不是直接从基类继承的。 If there is:如果有:

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

   public Fruit()
   {
   }
}

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

   public Citrus()
   {
   }
}

I don't seem to be able to set GroupFruit in the Citrus class.我似乎无法在 Citrus 类中设置 GroupFruit。 Thanks.谢谢。

Jerome杰罗姆

You need to initialize it in the constructor of the child class and it must be at least protected :您需要在子类的构造函数中初始化它,并且它必须至少protected

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

    public Fruit()
    {
    }
}

public class Citrus : Fruit
{

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM