vb.net 你可以在VB中继承一个带有参数的子新(构造函数)吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/789814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:11:44  来源:igfitidea点击:

Can you inherit a sub new (Constructor) with parameters in VB?

vb.netinheritanceconstructornew-operator

提问by Bentley Davis

In the code below I receive the compile error

在下面的代码中,我收到编译错误

Error Too many arguments to 'Public Sub New()'

on the Dim TestChild As ChildClass = New ChildClass("c"). I do not receive it on TestChild.Method1()even though they are both on the base class I am inheriting from.

Dim TestChild As ChildClass = New ChildClass("c")TestChild.Method1()即使它们都在我继承的基类上,我也没有收到它。

Public Class BaseClass
    Public ReadOnly Text As String
    Public Sub New(ByVal SetText As String)
        Text = SetText
    End Sub
    Public Sub New()
        Text = ""
    End Sub
End Class

Public Class ChildClass
    Inherits BaseClass
End Class

Public Class TestClass
    Sub Test()
        Dim TestChild As ChildClass = New ChildClass("c")
        TestChild.Method1()
    End Sub
End Class

I could change the child class to:

我可以将子类更改为:

Public Class ChildClass
    Inherits BaseClass
      Public Sub New (ByVal SetText As String)
      MyBase.New(SetText)
    End Class
End Class

As suggested below but I do not have to do that for Method 1 or other inherited methods and I am looking for the cleanest code possible. This may be a limitation in the system with inheriting parameterized New statements but I can not find it documented anywhere. If it is required then I would like to see the documentation.

如下所示,但我不必对方法 1 或其他继承的方法执行此操作,并且我正在寻找可能的最干净的代码。这可能是继承参数化 New 语句的系统中的一个限制,但我在任何地方都找不到它的记录。如果需要,那么我想查看文档。

回答by JaredPar

The behavior that you are seeing is "By Design". Child classes do not inherti constructors from their base types. A child class is responsible for defining it's own constructors. Additionally it must ensure that each constructor it defines either implicitly or explicitly calls into a base class constructor or chains to another constructor in the same type.

您看到的行为是“按设计”。子类不会从它们的基类型继承构造函数。子类负责定义它自己的构造函数。此外,它必须确保它定义的每个构造函数隐式或显式调用基类构造函数或链接到相同类型的另一个构造函数。

You will need to define the same constructor on all of the child classes and explicitly chain back into the base constructor via MyBase.New. Example

您需要在所有子类上定义相同的构造函数,并通过 MyBase.New 显式链接回基本构造函数。例子

Class ChildClass
  Inherits BaseClass
  Public Sub New(text As String)
    MyBase.New(text)
  End Sub
End Class

The documentation you are looking for is section 9.3.1 of the VB Language specification.

您正在寻找的文档是 VB 语言规范的第 9.3.1 节。

I think the most relevant section is the following (roughly start of the second page)

我认为最相关的部分如下(大约在第二页的开头)

If a type contains no instance constructor declarations, a default constructor is automatically provided. The default constructor simply invokes the parameterless constructor of the direct base type.

如果类型不包含实例构造函数声明,则会自动提供默认构造函数。默认构造函数只是调用直接基类型的无参数构造函数。

This does not explicitly state that a child class will not inherit constructors but it's a side effect of the statement.

这并未明确声明子类不会继承构造函数,但这是该语句的副作用。

回答by Jose Basilio

Parameterized Constructors cannot be inherited in the same way as instance methods. You need to implement the constructor in the child class and then call the parent's constructor using MyBase.

参数化构造函数不能以与实例方法相同的方式继承。您需要在子类中实现构造函数,然后使用 MyBase 调用父类的构造函数。

Public Class ChildClass
    Inherits BaseClass

    Public Sub New (ByVal SetText As String)
      MyBase.New(SetText)
    End Class
End Class

Public Class TestClass
  Public TestChild AS New ChildClass("c")
End Class

This limitation is not VB specific. From what I can gather, it's definately not possible in C#, Java or C++ either.

此限制不是特定于 VB 的。据我所知,在 C#、Java 或 C++ 中也绝对不可能。

Here is one related post with the same question about C++:
c-superclass-constructor-calling-rules

这是一篇有关 C++ 的相同问题的相关帖子:
c-superclass-constructor-calling-rules