为什么接口(interface)会覆盖类定义并违反类封装?我在下面包含了两个示例,一个在 C# 中,一个在 VB.net 中?
VB.net
Module Module1
Sub Main()
Dim testInterface As ITest = New TestMe
Console.WriteLine(testInterface.Testable) ''// Prints False
testInterface.Testable = True ''// Access to Private!!!
Console.WriteLine(testInterface.Testable) ''// Prints True
Dim testClass As TestMe = New TestMe
Console.WriteLine(testClass.Testable) ''// Prints False
''//testClass.Testable = True ''// Compile Error
Console.WriteLine(testClass.Testable) ''// Prints False
End Sub
End Module
Public Class TestMe : Implements ITest
Private m_testable As Boolean = False
Public Property Testable As Boolean Implements ITest.Testable
Get
Return m_testable
End Get
Private Set(ByVal value As Boolean)
m_testable = value
End Set
End Property
End Class
Interface ITest
Property Testable As Boolean
End Interface
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceCSTest
{
class Program
{
static void Main(string[] args)
{
ITest testInterface = new TestMe();
Console.WriteLine(testInterface.Testable);
testInterface.Testable = true;
Console.WriteLine(testInterface.Testable);
TestMe testClass = new TestMe();
Console.WriteLine(testClass.Testable);
//testClass.Testable = true;
Console.WriteLine(testClass.Testable);
}
}
class TestMe : ITest
{
private bool m_testable = false;
public bool Testable
{
get
{
return m_testable;
}
private set
{
m_testable = value;
}
}
}
interface ITest
{
bool Testable { get; set; }
}
}
更具体
如何在 VB.net 中实现一个允许私有(private) setter 的接口(interface)。例如在 C# 中我可以声明:
class TestMe : ITest
{
private bool m_testable = false;
public bool Testable
{
get
{
return m_testable;
}
private set //No Compile Error here!
{
m_testable = value;
}
}
}
interface ITest
{
bool Testable { get; }
}
但是,如果我在 VB.net 中将 interface 属性声明为 readonly,则无法创建 setter。如果我创建一个 VB.net interface 作为普通的旧 property,那么接口(interface)声明将违反我的封装。
Public Class TestMe : Implements ITest
Private m_testable As Boolean = False
Public ReadOnly Property Testable As Boolean Implements ITest.Testable
Get
Return m_testable
End Get
Private Set(ByVal value As Boolean) ''//Compile Error
m_testable = value
End Set
End Property
End Class
Interface ITest
ReadOnly Property Testable As Boolean
End Interface
所以我的问题是,如何通过适当的封装在 VB.net 中定义一个只有 getter 的接口(interface)?
我认为第一个例子是最好的方法。但是,接口(interface)定义似乎凌驾于类定义之上。所以我尝试像在 C# 中一样创建一个getter only (Readonly) property,但它不适用于 VB.net。也许这只是语言的限制?
更新
根据 Hans Passant 的评论,我提交了一个功能请求:https://connect.microsoft.com/VisualStudio/feedback/details/635591/create-a-readonly-interface-that-allows-private-setters-c-to-vb-net-conversion
如果您也想要相同的兼容性功能,请为它投票!
最佳答案
下面是我在 VB.NET 中的做法:
Public Interface ITest
ReadOnly Property Testable As Boolean
End Interface
Public Class Test
Implements ITest
' Note: Here I am NOT implementing the interface. '
Private _testable As Boolean
Public Property Testable() As Boolean
Get
Return _testable
End Get
Private Set(ByVal value As Boolean)
_testable = value
End Set
End Property
' This is where I define a read-only property to satisfy the interface '
' (from the perspective of the VB compiler). '
' Notice this is a lot like explicit interface implementation in C#. '
Private ReadOnly Property TestableExplicit() As Boolean Implements ITest.Testable
Get
Return Testable
End Get
End Property
End Class
关于c# - .NET:接口(interface)问题 VB.net Getter Only Interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4662352/