构造函数参数和继承 [英] Constructor Parameters and Inheritance

查看:113
本文介绍了构造函数参数和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的OOP和我很困惑的派生类构造函数如何工作时继承从C#中的基类。

New to OOP and I'm confused by how derived-class constructors work when inheriting from a base class in C#.

首先是基类:

class BaseClass
{
    private string BaseOutput = null;

    public BaseClass(string BaseString)
    {
        BaseOutput = BaseString;
    }

    public virtual void PrintLine()
    {
        Console.WriteLine(BaseOutput);
    }
}

这是派生类:

class SubClass : BaseClass
{
    private string SubOutput = null;

    public SubClass(string BaseString, string SubString) : base(BaseString)
    {
        SubOutput = SubString;
    }

    public override void PrintLine()
    {
        Console.WriteLine(SubOutput);
    }
}

最后,程序的主要部分: p>

Finally, the main part of the program:

class Program
{
    static void Main(string[] args)
    {
        BaseClass theBase = new BaseClass("Text for BaseClass");
        SubClass theSub = new SubClass("2nd param", "Text for SubClass");

        theBase.PrintLine();
        theSub.PrintLine();

        Console.ReadKey();
    }
}

我不知道为什么,派生类的构造函数,我还必须传递基类需要的参数。如果没有为其分配值,派生类中的BaseOutput字段是否应该保持设置为null?为什么不能这样工作:

What I don't get is why, when calling the constructor for the derived class, I have to also pass the parameter that the base class needs. Shouldn't the BaseOutput field in the derived class just stay set to null if no value is assigned to it? Why can't something like this work:

public SubClass(string SubString) : base(BaseString)

此外,当调用此派生类中的构造函数时,第一个参数必须与基类中的名称相同否则它抛出一个错误。如果我在派生类中定义一个新的字符串变量AnotherString,为什么不工作:

Furthermore, when calling the constructor in this derived class, the first parameter has to be named the same as the one in the base class or else it throws an error. If I were to define a new string variable called AnotherString in the derived class, why wouldn't this work:

public SubClass(string AnotherString, string SubString) : base(BaseString)



最后,当你这样做输出这个...

Lastly, when you do this the right way and type out this...

public SubClass(string BaseString, string SubString) : base(BaseString)

... SubClass构造函数中的第一个参数是什么?它不是在我的派生类的任何方法中分配或使用。

...what is the first parameter in the SubClass constructor used for? It's not being assigned or used in any methods for my derived class. Why do I have to even put it there at all?

推荐答案

至于为什么你不能这样做:

As to why you can't do:

public SubClass(string SubString) : base(BaseString)

BaseString 是什么?

>

You could do:

public SubClass(string SubString) : base("SomeFixedString")

public SubClass(string SubString) : base(SubString)

但是如果你想传递一个字符串给基类构造函数的参数,需要接受两个参数。

But if you want to pass one string to the base class constructor's parameter and have an additional one, you'll need to accept two parameters.

至于保持相同的名称,您不要。你可以这样做:

As to keeping the same name, you don't. You could do:

public SubClass(string AnotherString, string SubString) : base(AnotherString)

对于最后一个问题,第一个参数不是什么,它被传递给基类构造函数。如果你想的话,你可以用它来做其他事情。

As to the last question, the first parameter isn't doing nothing, it's being passed to the base class constructor. You could use it for something else if you wanted to.

这篇关于构造函数参数和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆