VB.NET中构造函数的构造函数是如何定义的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计265个文字,预计阅读时间需要2分钟。
这是否是一个错误的主意?在公共构造函数中调用泛型私有构造函数是否会导致创建多个实例,或者这是初始化类变量的一种有效方法?+Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'D'
Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'Do stuff End Sub Sub New(ByVal name As String, ByVal age As Integer) Me.New() 'Do stuff End Sub Private Sub New() 'Initializer constructor Me._msg = "Hello StackOverflow" 'Initialize other variables End Sub End Class 这是一种有效的方法.有一些警告可以调用新函数:
The Sub New constructor can run only once when a class is created. It
cannot be called explicitly anywhere other than in the first line of
code of another constructor from either the same class or from a
derived class.
在MSDN上阅读有关object lifetime的更多信息.
本文共计265个文字,预计阅读时间需要2分钟。
这是否是一个错误的主意?在公共构造函数中调用泛型私有构造函数是否会导致创建多个实例,或者这是初始化类变量的一种有效方法?+Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'D'
Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'Do stuff End Sub Sub New(ByVal name As String, ByVal age As Integer) Me.New() 'Do stuff End Sub Private Sub New() 'Initializer constructor Me._msg = "Hello StackOverflow" 'Initialize other variables End Sub End Class 这是一种有效的方法.有一些警告可以调用新函数:
The Sub New constructor can run only once when a class is created. It
cannot be called explicitly anywhere other than in the first line of
code of another constructor from either the same class or from a
derived class.
在MSDN上阅读有关object lifetime的更多信息.

