类中的成员是其自身的实例的问题:
我在学习《VB.NET设计模式高级编程》中看到一段这样的例程:
Public MustInherit Class State
Protected Sub New()
End Sub
MustOverride Sub GoToAlberta(ByRef tm As TravelManager)
MustOverride Sub GoToHawaii(ByRef tm As TravelManager)
MustOverride Sub GoToWashington(ByRef tm As TravelManager)
Protected Sub ChangeState(ByRef tm As TravelManager, ByRef s As State)
tm.ChangeState(s)
End Sub
End Class
Public Class Alberta : Inherits State
'引用Alberta类自身的实例做为一个成员
Private Shared m_state = New alberta
Private Sub New()
End Sub
Public Shared Function Instance() As State
console.writeline("Welcome to Alberta!")
Return m_State
End Function
Public Overrides Sub GoToAlberta(ByRef tm As TravelManager)
console.writeline("You are already in Alberta.")
End Sub
Public Overrides Sub GoToHawaii(ByRef tm As TravelManager)
Console.WriteLine("Enjoy your flight to Hawaii.")
ChangeState(tm, Hawaii.Instance)
End Sub
Public Overrides Sub GoToWashington(ByRef tm As TravelManager)
Console.WriteLine("Enjoy the drive to America.")
changestate(tm, Washington.instance)
End Sub
End Class
请注意红色的代码,上面的类成员是这个类的一个实例,这样的类如果实例化为什么不会出现问题,当然我知道享元的成员只会
实例化一次,问题是对象引用自身对象难到不会出问题吗?