假设我有这个父类:
Public MustInherit Class Parent
' ReadOnly instance variables:
Protected ReadOnly str1 As String
Protected ReadOnly str2 As String
Protected ReadOnly str3 As String
' constructor:
Public Sub New()
End Sub
End Class
我想在子类的构造函数中分配这些变量,并且我希望它们ReadOnly
一旦分配就不能更改,如下所示:
Public Class Child
Inherits Parent
' constructor:
Public Sub New()
MyBase.New()
' can't assign the ReadOnly variables here!
' compile error: 'ReadOnly' variable cannot be the target of an assignment
Me.str1 = "asdf"
Me.str2 = "qwerty"
Me.str3 = "foobar"
End Sub
End Class
我怎样才能做到这一点?如果不可能,为什么不呢?