我正在尝试从覆盖的公共变量中检索一个值,但令人惊讶的是从私有属性中获取值?
在下面找到代码,
项目 - 接口 - 包含 - IAnimal,IPoo
项目 - ML - 包含 - AnimalBase,Cat,Dog,Poo,RadioactivePoo
项目 - ConsoleApplication - 包含 - 模块
Public Interface IAnimal
ReadOnly Property Excrement() As List(Of Poo)
Sub Eat()
End Interface
Public Class Poo
End Class
Public Class RadioactivePoo
End Class
Public Class AnimalBase(Of T)
Implements IAnimal
Private _Excrement As List(Of Poo)
Public Overridable ReadOnly Property Excrement() As List(Of T)
Get
Return Nothing
End Get
End Property
Public Overridable Sub Eat() Implements IAnimal.Eat
If _Excrement Is Nothing Then
_Excrement = New List(Of Poo)
End If
_Excrement.Add(New Poo)
End Sub
Private ReadOnly Property Excrement1 As List(Of Poo) Implements IAnimal.Excrement
Get
Return Nothing
End Get
End Property
End Class
Public Class Dog
Inherits AnimalBase(Of Poo)
Private _Excrement As List(Of Poo)
Public Overrides ReadOnly Property Excrement() As List(Of Poo)
Get
Return _Excrement
End Get
End Property
Public Overrides Sub Eat()
If _Excrement Is Nothing Then
_Excrement = New List(Of Poo)
End If
_Excrement.Add(New Poo)
End Sub
End Class
Public Class Cat
Inherits AnimalBase(Of RadioactivePoo)
Private _Excrement As List(Of RadioactivePoo)
Public Overrides ReadOnly Property Excrement() As List(Of RadioactivePoo)
Get
Return _Excrement
End Get
End Property
Public Overrides Sub Eat()
If _Excrement Is Nothing Then
_Excrement = New List(Of RadioactivePoo)
End If
_Excrement.Add(New RadioactivePoo)
End Sub
End Class
Module Module1
Sub Main()
Dim dog As IAnimal = New Dog
dog.Eat()
'Dim dogPoo As Poo = dog.Excrement
Dim cat As IAnimal = New Cat()
cat.Eat()
'Dim catPoo As RadioactivePoo = cat.StronglyTypedExcrement()
Console.WriteLine(dog.Excrement.ToString)
Console.WriteLine(cat.Excrement.ToString)
Console.Read()
End Sub
End Module
我正在尝试从dog.Excrement / cat.Excrement中检索值,但令人惊讶的是它从dog.Excrement1(它是私有的,但由IAnimal类实现)获得它。
如何从粪便中获取价值? ,但是当我试图访问时,它什么也没有返回。 see the pic, which lists the values, where you can see the public Excrement function has value, and private has nothing