在VB.NET中处理构造函数时值得注意的两个陈述
在VB.NET中处理构造函数时,有两个陈述值得注意:
(1) 如果类型不包含任何实例构造函数声明,则VB.NET提供默认的构造函数;
默认参数调用直接基类的无参数构造函数。如果基类没有可访问的无参数构造函数,则发生编译期错误。
(2) 构造函数不能继承;
陈述(1)包含两个方面的信息:①是如果类型不包含任何实例构造函数声明,则VB.NET提供默认的构造函数,即下面的声明是允许的,在类Shape中我们没有显式声明任何构造函数,但是我们仍然可以通过VB.NET自动提供的无参数构造函数New()来实例化类;
Module Test
Public Sub Main()
Dim shape1 As Shape = New Shape
End Sub
End Module
Public Class Shape
Public Sub Draw()
'some codes here.
End Sub
End Class
②是如果类型中包含有实例构造函数的声明,那么VB.NET不再提供默认的构造函数,例如,我们在类Shape中添加了一个带参数的构造函数,
Public Class Shape
Private _pt As Point
Public Sub New(p As Point)
_pt = p
End Sub
Public Sub Draw()
'some codes here.
End Sub
End Class
那么,如果直接实例化类时,则会产生错误;
Dim shape1 As Shape = New Shape
只能通过调用Shape类中显式声明的带参数的构造函数来实例化类,如下,
Dim shape1 As Shape = New Shape(New Point(0, 0))
因此,如果要在类中同时使用无参数和带参数的构造函数,必须同时显式地声明两种构造函数。
Public Class Shape
Private _pt As Point
Public Sub New()
'some codes here.
End Sub
Public Sub New(p As Point)
_pt = p
End Sub
Public Sub Draw()
'some codes here.
End Sub
End Class
陈述(2),构造函数不能继承,即,如果要使子类与基类有相同的构造函数,那么必须在每个子类中都显式的声明与基类构造相同的构造函数,例如:
Public MustInherit Class Shape
Private _pt As Point
Public Sub New()
'some codes here.
End Sub
Public Sub New(p As Point)
_pt = p
End Sub
Public MustOverride Sub Draw()
End Class
Public Class Line
Inherits Shape
Public Sub New()
End Sub
Public Sub New(p As Point)
MyBase.New(p)
End Sub
Public Overrides Sub Draw()
'some codes here.
End Sub
End Class
这一点非常重要而且也很有意义。因为我们往往倾向于认为子类继承了基类所有的属性和方法,这往往导致我们的程序出现编译错误。
综合以上两个陈述,在实际编程中应遵循一个原则:在类中,无论是基类还是子类,显式地声明所有需要的无参数或带参数构造函数。遵循这个原则,既有利于提高编译正确代码的速度,也有利于自己和其他程序员理解代码。
(1) 如果类型不包含任何实例构造函数声明,则VB.NET提供默认的构造函数;
默认参数调用直接基类的无参数构造函数。如果基类没有可访问的无参数构造函数,则发生编译期错误。
(2) 构造函数不能继承;
陈述(1)包含两个方面的信息:①是如果类型不包含任何实例构造函数声明,则VB.NET提供默认的构造函数,即下面的声明是允许的,在类Shape中我们没有显式声明任何构造函数,但是我们仍然可以通过VB.NET自动提供的无参数构造函数New()来实例化类;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
②是如果类型中包含有实例构造函数的声明,那么VB.NET不再提供默认的构造函数,例如,我们在类Shape中添加了一个带参数的构造函数,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
那么,如果直接实例化类时,则会产生错误;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
只能通过调用Shape类中显式声明的带参数的构造函数来实例化类,如下,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
因此,如果要在类中同时使用无参数和带参数的构造函数,必须同时显式地声明两种构造函数。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
陈述(2),构造函数不能继承,即,如果要使子类与基类有相同的构造函数,那么必须在每个子类中都显式的声明与基类构造相同的构造函数,例如:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这一点非常重要而且也很有意义。因为我们往往倾向于认为子类继承了基类所有的属性和方法,这往往导致我们的程序出现编译错误。
综合以上两个陈述,在实际编程中应遵循一个原则:在类中,无论是基类还是子类,显式地声明所有需要的无参数或带参数构造函数。遵循这个原则,既有利于提高编译正确代码的速度,也有利于自己和其他程序员理解代码。