Mik*_* L. 1 .net vb.net inheritance
我创建了一个继承List(Of TKey, TValue)
并添加一些函数的类.它的目的不是在运行时向它添加项目,而是在初始化时.我实际上想从类中删除add/delete方法(因为它当前从继承的类中公开它们).
如果你看到TestApp
你会看到的课程,如果我List(Of String, Integer)
在课堂上添加一个它就可以了.但是,在下面提供的代码中,当我尝试Me.Add()
在类构造函数中使用时; 添加它不起作用.它实际上告诉我,String不能转换为TKey,并且Integer不能转换为TValue.这是不真实的!它在测试应用程序中工作正常.
有任何想法吗?这是代码.两个类,Vars类和Test应用程序.
注意:即使Add
在类中使用Overrides 方法,Me.Add(String, Integer)
也不起作用(相同的错误)
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Vars(Of TKey, TValue)
Inherits List(Of KeyValuePair(Of TKey, TValue))
''' <summary>
''' Initializes the class and fills with Key->Value Pairs
''' </summary>
''' <remarks>
''' This does not work when adding directly from the class. When I overload
''' the Add() function, and call it with a KeyValuePair(Of String, String) in a
''' class instance it works, see below
''' </remarks>
Public Sub New()
Dim kv = New KeyValuePair(Of TKey, TValue)("This", 1)
Me.Add(kv)
End Sub
''' <summary>
''' Adds an item to the class by String, Integer
''' </summary>
''' <param name="aKey">A String containing the Key of the element to be added.</param>
''' <param name="aValue">An Integer containing the Value of the element to be added.</param>
''' <remarks>
''' This Works fine when called as in the demo shown below.
''' </remarks>
Public Overloads Sub Add(ByVal aKey As TKey, ByVal aValue As TValue)
Dim kv = New KeyValuePair(Of TKey, TValue)(aKey, aValue)
Me.Add(kv)
End Sub
''' <summary>
''' Finds a Value stored in the class based on a given Key.
''' </summary>
''' <param name="key">A String containing a Key to search for.</param>
''' <remarks>Works.</remarks>
Public Function FindByKey(ByVal key As TKey) As List(Of KeyValuePair(Of TKey, TValue))
Return Me.FindAll(Function(item As KeyValuePair(Of TKey, TValue)) (item.Key.Equals(key)))
End Function
''' <summary>
''' Finds a Key stored in the class based on a given Value.
''' </summary>
''' <param name="value">An Integer containing the Value to search for.</param>
''' <remarks>Works</remarks>
Public Function FindByValue(ByVal value As TValue) As List(Of KeyValuePair(Of TKey, TValue))
Return Me.FindAll(Function(item As KeyValuePair(Of TKey, TValue)) (item.Value.Equals(value)))
End Function
End Class
Public Class TestApp
Shared Sub Main()
Dim varTest As Vars(Of String, Integer) = New Vars(Of String, Integer)
varTest.Add("Kung", 1)
varTest.Add("Foo", 2)
For Each var In varTest
Console.WriteLine(var.Key & ":" & var.Value)
Next
'Output would be
'
' Kung:1
' Foo:2
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
这里发生了很多事情.
具体做法是:在类意图是通用?也就是说,你要使用Vars(Of String, Integer)
,Vars(Of Object,
DateTime)
,Vars(Of Double, Object)
以及种种?如果是这样,那么你想在这一行上发生什么:
Dim kv = New KeyValuePair(Of TKey, TValue)("This", 1)
Run Code Online (Sandbox Code Playgroud)
当TKey
是不 String
和TValue
是不是 Integer
?你可以添加到a的唯一的东西List(Of KeyValuePair(Of TKey, TValue))
是KeyValuePair(Of TKey, TValue)
,而不是任何旧的
KeyValuePair
.
更一般地说:List(Of T)
不是为了继承而设计的(这很多,这只是我发现的第一个示例答案).一定要让你的类包含一个List(Of T)
,并保留其中的项目,而不是继承,你应该确切地决定你希望你的类的公共接口是什么,并只实现它.这
可能是执行IList
或其他一些预先定义的界面让你想要的合同,但不一定.
更一般地说:当你发现自己说"我真的想从类中删除添加/删除方法"时,这是一个很大的提示,你不应该从该类继承.在
里氏替换原则说(大致)任何东西,从一个类继承C
应该能够像对待C
:如果从继承List
,呼叫者会很自然地希望能Add
和Delete
.如果您不希望在公共接口上使用这些方法,请不要从该类继承.
归档时间: |
|
查看次数: |
227 次 |
最近记录: |