你可以继承一个带有参数在VB中的子新(构造函数)? [英] Can you inherit a sub new (Constructor) with parameters in VB?

查看:598
本文介绍了你可以继承一个带有参数在VB中的子新(构造函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the code below I receive the compile error

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



Dim TestChild As ChildClass = New ChildClass(c)。我不会收到 TestChild.Method1(),即使他们都在我继承的基类上。

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.

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

我可以将子类更改为:

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

如下所示,但我没有为方法1或其他继承的方法,我正在寻找最干净的代码可能。这可能是在系统中继承参数化New语句的限制,但我不能在任何地方找到它。如果需要,我想看看文档。

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.

推荐答案

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

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.

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

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

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

寻找是VB语言规范的第9.3.1节。

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

  • http://msdn.microsoft.com/en-us/library/aa711964(VS.71).aspx

我认为最相关的部分是以下(大致第二页的开头)

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.

这篇关于你可以继承一个带有参数在VB中的子新(构造函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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