【发布时间】:2016-07-21 08:31:03
【问题描述】:
我想编写如下所示的代码:
Public Class LogFile
Inherits StreamWriter
Private LogsDirectory As String = Application.StartupPath & "\" & "logs\"
Public Sub New(shortName As String)
Dim fullFilePath As String = LogsDirectory & shortName & "_" & Format(Now, "HHmmss") & ".log"
MyBase.New(fullFilePath)
End Sub
End Class
但是我不得不这样做(因为Sub New 的第一条语句必须是基础构造函数):
Public Class LogFile
Inherits StreamWriter
Public Sub New(shortName As String)
MyBase.New(Application.StartupPath & "\" & "logs\" & shortName & "_" & Format(Now, "HHmmss") & ".log")
End Sub
End Class
有没有办法“解决”这个问题?在这种情况下没关系,但可以想象我想做更多的处理,而不是一行。
【问题讨论】:
-
LogsDirectory 变量必须先赋值,然后才能使用。看起来它在代码中是自动的,但这只是语法糖,构造函数负责处理。但是等等,你正在编写构造函数。鸡蛋必须先于鸡。这同样适用于 MyBase.New()。
标签: .net vb.net inheritance streamwriter