我有一个接口IDigitalState定义为限制对结构的接口只有在vb.net
Public Interface IDigitalState
ReadOnly Property Code As Integer
ReadOnly Property Name As String
End Interface
,并实现该接口
Public Structure DigitalState
Implements IDigitalState
Private ReadOnly mCode As Integer
Private ReadOnly mName As String
Public ReadOnly Property Code As Integer Implements IDigitalState.Code
Get
Return mCode
End Get
End Property
Public ReadOnly Property Name As String Implements IDigitalState.Name
Get
Return mName
End Get
End Property
Public Sub New(ByVal code As Integer, name As String)
mCode = code
mName = name
End Sub
End Structure
我想做一个结构是声明一个变量作为IDigitalState的可空类型。我明白为什么我不能这样做,因为接口可能由一个不允许为空的类来实现。有没有一种方法来定义接口,以便它只能通过结构来实现。我怀疑这是可能的,但认为这是值得研究的。
你也许可以找到一个解决这个问题的方法,使用泛型和T:struct,但我认为你希望你的界面对实现者施加比习惯更强的控制权。 – hatchet