关于C#构造函数的几个问题

发布于 2024-10-30 02:23:22 字数 170 浏览 4 评论 0原文

在C#中关于构造函数的继承:

  1. 我读到构造函数不能被继承。
  2. 如果基类包含一个或多个构造函数,派生类必须始终调用其中之一吗?派生类不可能跳过基类的构造函数吗?
  3. 派生类可以有自己的构造函数,但它必须调用基类构造函数之一。

这些说法正确吗?

In C# regarding the inheritance of constructors:

  1. I have read that constructors cannot be inherited.
  2. If the base class contains a constructor, one or more, the derived class have to always call one of them? It is not possible that the derived class can skip the constructor of the base class?
  3. Derived class can have its own constructor but it has to call one of the base class constructors.

Are these statements corrects?

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

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

发布评论

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

评论(3

谜兔 2024-11-06 02:23:22
  1. 是的,你是对的,构造函数不是继承的。因此,仅仅因为 Object 有一个无参数构造函数,并不意味着 String 有一个无参数构造函数。来自 C# 4 规范第 1.6.7.1 节:

    <块引用>

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

  2. 是的,派生类构造函数的构造函数链将始终通过其基类构造函数...尽管它可能是间接的,因为它可以通过 this(... 链接到同一类中的另一个构造函数。 ) 而不是 base(...)。如果您未指定 this(...)base(...),则相当于 base() - 调用基类中的无参数构造函数。

有关详细信息,请参阅我的有关构造函数链的文章

  1. Yes, you're right, constructors are not inherited. So just because Object has a parameterless constructor, that doesn't mean that String has a parameterless constructor, for example. From section 1.6.7.1 of the C# 4 spec:

    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.

  2. Yes, the constructor chain of a derived class constructor will always go through its base class constructor... although it may be indirectly, as it could chain to another constructor in the same class via this(...) instead of base(...). If you don't specify either this(...) or base(...), that's equivalent to base() - calling the parameterless constructor in the base class.

See my article on constructor chaining for more information.

在梵高的星空下 2024-11-06 02:23:22

1.不能继承构造函数,但您可以链接构造函数。示例:

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

2. 是的,例如,您无法实例化从具有所有私有构造函数的对象派生的类(除了 Jon 在下面的评论中提到的特定场景) 。您不能跳过基类的构造函数,但可以有多个重载,并选择您选择使用的构造函数。

3.是的,不确定这里的问题是什么。

1. You cannot inherit constructors, but you can chain constructors. Example:

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

2. Yes, for example, you cannot instantiate a class derived from an object with all private constructors (except in the particular scenario that Jon mentions below in his comment). You cannot skip the constructor of a base class, but you can have multiple overloads, and select the constructor you choose to use.

3. Yes, not sure what the question is here.

紧拥背影 2024-11-06 02:23:22

1 是

2 你不能跳过构造函数,但我认为你不能在默认构造函数中执行任何操作。就像

public class PClass {

    protected PClass() {
        // do nothing
    }

    public PClass(int myIntArg) {
        // initialize the class
    }

}

public Class CClass : PClass {

    public CClass(string myStringArg) : base() {
        // initialize the class
    }

}

3 是的。看我之前的例子。

1 Yes

2 You cannot skip constructor but I think you could do nothing in default constructor. Just like

public class PClass {

    protected PClass() {
        // do nothing
    }

    public PClass(int myIntArg) {
        // initialize the class
    }

}

public Class CClass : PClass {

    public CClass(string myStringArg) : base() {
        // initialize the class
    }

}

3 Yes. See my previous example.

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