【问题标题】:how to provide value to parent class constructor where parent constructor have more argument than child class from static void main in c#? [closed]如何为父类构造函数提供值,其中父构造函数的参数比c#中静态void main的子类多? [关闭]
【发布时间】:2019-09-04 09:16:49
【问题描述】:

我有一个父类 Customer,它有 3 个 properties **

CID,Bal,Cname

**
我有一个具有 1 个属性 Stats 的子类。现在我想通过静态 void main 从我的子类构造函数向父类 constructor 提供值。
我想从基类向父类构造函数提供值只要。 我的代码如下

   static void Main(string[] args)
        {
            Stat s1 = new Stat(false);//want to provide value to base class constructor from here only.
        }
    }

    class Customer
    {
        int _Cid, _Bal;

        string _Cname;

        public int CID
        {
            get
            {
                return _Cid;
            }
            set
            {
                _Cid= value;
            }
        }

        public int Bal
        {
            get
            {
                return _Bal;
            }
            set
            {
                _Bal = value;
            }
        }
        public string Cname
        {
            get
            {
                return _Cname;
            }
            set
            {
                _Cname = value;
            }
        }

        public Customer(int _Cid,int _Bal,String _Cname)
        {
            this._Cid=_Cid;
            this._Cname = _Cname;
            this._Bal = _Bal;
        }
    }


    class Stat:Customer
    {
        bool _Status;

        public bool Stats
            {
            get
            {
                return _Status;
            }
            set
            {
                _Status= value;
            }
    }
        public void display()
        {
        }
        public Stat(bool _Status):base(int _Cid, int _Bal, String _Cname) //child class constructor how can i supply value to parent class constructor.
        {
            this._Status = _Status;
        }
    }

【问题讨论】:

  • 您必须定义一个接受正确值的派生类构造函数,然后传递它们。
  • 已经有@MarkBenningfield
  • 不,你没有。发布的代码甚至无法编译。
  • 是的,因为我不知道如何为父类构造函数@MarkBenningfield 提供值

标签: c# inheritance properties


【解决方案1】:

你的基类构造函数是这样的:

public Customer(int _Cid,int _Bal,String _Cname)

你的派生类构造函数是这样的:

public Stat(bool _Status)

在 C# 中,当您实例化派生类时,必须调用基类。在基类只有一个无参数构造函数的情况下,这是在派生类构造函数体执行之前隐式完成的。如果基类没有无参数构造函数,则必须使用base 显式调用它。

举个例子:

public enum MyEnum
{
    Person,
    Animal,
    Derived
}

public class Base
{
    public Base(MyEnum classType)
    {
    }
}

public class Person : Base
{

}

有两种方法可以做到这一点:接受Person 的参数并将其传递给基本构造函数:

public class Person : Base
{
    public Person(MyEnum classType) : base(classType)
    {
        // this will be executed after the base constructor completes
    }
}

或通过对值进行硬编码(假设 MyEnum 包含一个值 Person):

public class Person : Base
{
    public Person() : base(MyEnum.Person)
    {
        // this will be executed after the base constructor completes
    }
}

请注意,您可以有多个构造函数,因此如果派生类应该使用一些默认值来实例化这些值,您可以定义一个不同的protected 构造函数供派生类使用。 protected 确保它只能被派生类调用,而不能被任何调用 new Base(...) 的人调用:

public class Base
{
    private readonly MyEnum _classType;

    public Base(MyEnum classType)
    {
        _classType = classType;
    }

    protected Base()
    {
        _classType = classType.Derived;
    }
}

public class Person : Base
{
    public Person() : base()
    {
        // this will be executed after the base constructor completes
    }        
}

基类和派生类中的参数数量之间没有关系。只存在一个简单的要求:派生构造函数必须调用(并满足任何参数要求)其基类构造函数,其中基类构造函数接受参数,或者有多个参数。

在您的具体示例中,您可能打算这样做:

public Stat(bool _Status, int _Cid, int _Bal, String _Cname) : base(_Cid, _Bal, _Cname)

作为旁注,命名参数_Cid 有点奇怪。前缀_ 通常表示它是类中的私有字段。正常的 C# 约定使用驼峰式 (camelCaseArgumentName) 作为方法参数。

【讨论】:

  • 一个小修正:当基类只有一个无参数构造函数时,它被隐式调用。如果它还有另一个构造函数,则必须在子类构造函数中显式调用其中一个。
  • @Zohar 好点。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 2010-12-06
  • 2018-02-28
  • 1970-01-01
  • 2014-12-16
  • 2018-10-17
相关资源
最近更新 更多