如何在基类中声明构造函数,以便子类无需声明也可以使用它们?

发布于 2024-12-27 08:21:01 字数 195 浏览 0 评论 0原文

我希望子类使用其父类的构造函数。但似乎我总是需要在子类中再次定义它们才能工作,如下所示:

public SubClass(int x, int y) : base (x, y) {
    //no code here
}

所以我想知道我是否没有在父类中正确声明构造函数,或者是否没有直接的构造函数继承全部?

I want a subclass to use its parent's constructors. But it seems I always need to define them again in the subclass in order for that to work, like so:

public SubClass(int x, int y) : base (x, y) {
    //no code here
}

So I'm wondering if I'm not declaring the constructor properly in the parent class, or is there no direct constructor inheritance at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫轩蝶泪 2025-01-03 08:21:01

你没有做错任何事。

在 C# 中,实例构造函数不会被继承,因此在继承类型上声明它们并链接到基本构造函数是正确的方法。

根据规范§1.6.7.1:

与其他成员不同,实例构造函数不是继承的,并且类除了在类中实际声明的实例构造函数之外没有实例构造函数。如果没有为类提供实例构造函数,则会自动提供一个不带参数的空实例构造函数。

You are not doing anything wrong.

In C#, instance constructors do not get inherited, so declaring them on the inheriting type and chaining to the base constructor is the right way about it.

From the spec §1.6.7.1:

Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

旧时模样 2025-01-03 08:21:01

我知道这并不能直接回答你的问题;但是,如果大多数构造函数只是在前一个构造函数上引入一个新参数,那么您可以利用可选参数(在 C# 4 中引入)来减少需要定义的构造函数的数量。

例如:

public class BaseClass
{
    private int x;
    private int y;

    public BaseClass()
        : this(0, 0)
    { }

    public BaseClass(int x)
        : this(x, 0)
    { }

    public BaseClass(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

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

    public DerivedClass(int x)
        : base(x)
    { }

    public DerivedClass(int x, int y)
        : base(x, y)
    { }
}

上面的内容可以简化为:

public class BaseClass
{
    private int x;
    private int y;

    public BaseClass(int x = 0, int y = 0)
    {
        this.x = x;
        this.y = y;
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass(int x = 0, int y = 0)
        : base(x, y)
    { }
}

并且它仍然允许您使用任意数量的参数初始化 BaseClassDerivedClass

var d1 = new DerivedClass();
var d2 = new DerivedClass(42);
var d3 = new DerivedClass(42, 43);

I know this doesn’t directly answer your question; however, if most of your constructors simply introduce a new parameter on the previous constructor, then you could take advantage of optional arguments (introduced in C# 4) to reduce the number of constructors you need to define.

For example:

public class BaseClass
{
    private int x;
    private int y;

    public BaseClass()
        : this(0, 0)
    { }

    public BaseClass(int x)
        : this(x, 0)
    { }

    public BaseClass(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

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

    public DerivedClass(int x)
        : base(x)
    { }

    public DerivedClass(int x, int y)
        : base(x, y)
    { }
}

The above can be reduced to:

public class BaseClass
{
    private int x;
    private int y;

    public BaseClass(int x = 0, int y = 0)
    {
        this.x = x;
        this.y = y;
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass(int x = 0, int y = 0)
        : base(x, y)
    { }
}

And it would still allow you to initialize both BaseClass and DerivedClass with any number of arguments:

var d1 = new DerivedClass();
var d2 = new DerivedClass(42);
var d3 = new DerivedClass(42, 43);
旧城空念 2025-01-03 08:21:01

构造函数不是从基类继承到派生类的。每个构造函数必须首先调用基类 ctor。编译器只知道如何调用无参数构造函数。如果基类中没有这样的ctor,则必须手动调用它。

Constructors aren't inherited from base class to derived. Each constructor must call base class ctor first. Compiler knows only how to call parameterless ctor. If there is not such ctor in base class, you have to call it manually.

铁憨憨 2025-01-03 08:21:01

所以我想知道我是否没有在中正确声明构造函数
父类,因为这看起来很愚蠢。

如果基类没有默认构造函数,则必须在子类中重新声明它。这就是 OOP 在 .NET 中的工作方式。

So I'm wondering if I'm not declaring the constructor properly in the
parent class, because this seems silly.

If the base class doesn't have a default constructor you must redeclare it in the child class. That's how OOP work in .NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文