简体   繁体   English

如何将基类作为输入参数传递给派生类的构造函数?

[英]How to pass a base class as input parameter to the constructor of the derived class?

I have a derived class which is derived from base class, 我有一个派生类,它是从基类派生的,

public class DerivedClass : BaseClass
{
    public DerivedClass () {}
    public DerivedClass ( BaseClass bc )
    {
        // The following line is invalid, but I want to achieve something like this
        this = bc;
        // Error: (this) Cannot assign <This> because this is read only 
        // Error2: (bc) Cannot implicitly convert Type `BaseClass` to `DerivedClass`
    }
}

public class BaseClass
{
    public string TestName { get; set; }
    public uint TestNumber { get; set; }

    public BaseClass () { }
}

then from the Main method, 然后从Main方法

static void Main ( string[] args )
{
     BaseClass bc = new BaseClass ();
     bc.TestName = "dummy test";
     bc.TestNumber = 007;

     DerivedClass dc = new DerivedClass ( bc );
 }

Now my questions are, 现在我的问题是

  1. How to assign base class properties with the derived class, please note it has to achieved through the constructor of the derived class(shown in the code : this = bc; )? 如何通过派生类分配基类属性,请注意,它必须通过派生类的构造函数来实现(如代码所示: this = bc; )?
  2. Is it a right coding practice, if not why? 如果不是,这是正确的编码实践吗?

Thanks! 谢谢!

this is read-only alias - remember to future you can do like this this是只读别名-记住以后您可以这样做

public class DerivedClass : BaseClass
{
    public DerivedClass () {}
    public DerivedClass ( BaseClass bc )
    {
        TestName=bc.TestName;
        TestNumber=bc.TestNumber;
    }
}

Is it a right coding practice, if not why? 如果不是,这是正确的编码实践吗? Depends in what you try achieve 取决于您尝试实现的目标

You cannot do that, you can't use this in the constructor which refers to the current instance because you're going to construct the object just now. 您无法做到这一点,您不能在引用当前实例的构造函数中使用this ,因为您将要立即构造该对象。 I'd provide appropriate constructors: 我将提供适当的构造函数:

public class DerivedClass : BaseClass
{
    public DerivedClass() { }
    public DerivedClass(string TestName, uint TestNumber)
    {
        base.TestName = TestName;
        base.TestNumber = TestNumber;
    }
    public DerivedClass(BaseClass bc) : this(bc.TestName, bc.TestNumber) { }
}

So if you're not using the BaseClass variable in your example at all you don't need it. 因此,如果您根本没有在示例中使用BaseClass变量,则根本不需要它。 It's pointless to create a base object just to initialize a child class object. 创建基础对象只是初始化子类对象是没有意义的。

It's a bad practice. 这是一个坏习惯。

That would mean your base class already represents whatever the derived class would be. 那意味着您的基类已经代表了任何派生类。

I'd recommend composition: 我建议组成:

public class TestData
{
    public string TestName { get; set; }
    public uint TestNumber { get; set; }
}

public class BaseClass
{
    protected TestData data;
    public BaseClass(TestData data)
    {
        this.data = data;
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass(TestData data)
       : base(data)
    {
    }
}

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

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