2015-03-02 73 views
0

下無法編譯:爲什麼我不能在VB.NET中爲「friend」屬性指定一個「protected」修飾符?

MustInherit Class Foo 
    Friend Property objects As List(Of Object) 
     Get 
      Return _objects 
     End Get 
     Protected Set(value As List(Of Object)) 
      _objects = value 
     End Set 
    End Property 
    Private Property _objects As New List(Of Object) 
End Class 

我收到錯誤消息:Access modifier 'Protected Friend' is not valid. The access modifier of 'Get' and 'Set' should be more restrictive than the property access level.。那麼,這是更嚴格的。 Foo的子類只能在同一個程序集中設置此屬性。

爲什麼不允許?

回答

2

那麼,這是更具限制性。在同一個程序集中,只有Foo的子類可以設置這個屬性。

不,這是不正確的。 Protected Friend意味着程序集中的任何人以及派生自該類(在當前程序集之外)的任何人都可以調用該屬性。也就是說,這是一個「或」操作,而不是「和」。 VB.Net沒有你正在尋找的「受保護和朋友」訪問說明符;只有「受保護或朋友」。

+0

很好的答案。我只聲明'_objects'受保護,'objects'爲'ReadOnly','Foo'爲'Friend'。 – 2015-03-02 23:45:40

相關問題