问题描述
我有一个名为Modify
的函数.就像这样:
I have a function called Modify
. It is delcared like so:
Public Function Modify(Of SIMType As {New, DAOBase})(ByVal obj As DAOBase) As Boolean
您可以看到此功能是通用的.它以对象为参数,该对象是DAOBase或DAOBase的子类.
You can see that this function is generic. It takes as a paramer a object that is a DAOBase or subclasses of DAOBase.
在Modify函数内部有一个调用,如下所示:
Inside the modify function there is a call like so:
DAOToGP(obj)
这就是多态性发挥作用的地方.我创建了四个左右的DAOBase
子类.我为每种类型写了DAOToGP()
.因此,在Modify()
函数中,当它调用DAOToGP(obj)
时,应该引入多态性,并且应该根据我传递给Modify()
的类型来调用DAOToGP()
的正确实现.
This is where the polymorphism comes into play. There are four or so subclasses I have created of DAOBase
. I have written a DAOToGP()
for each of these types. So in the Modify()
function, when it calls the DAOToGP(obj)
, polymorphism should kick in and it should call the correct implementation of DAOToGP()
depending on the type that I pass into Modify()
.
但是,出现以下错误:
Error 20 Overload resolution failed because no accessible 'DAOToGP' can be called without a narrowing conversion:
'Public Shared Function DAOToGP(distributor As Distributors) As Microsoft.Dynamics.GP.Vendor': Argument matching parameter 'distributor' narrows from 'SierraLib.DAOBase' to 'IMS.Distributors'.
'Public Shared Function DAOToGP(product As Products) As Microsoft.Dynamics.GP.SalesItem': Argument matching parameter 'product' narrows from 'SierraLib.DAOBase' to 'IMS.Products'. C:\Users\dvargo.SIERRAWOWIRES\Documents\Visual Studio 2010\Projects\SIM\Dev_2\SIM\IMS\DVSIMLib\GP\GPSIMRunner\Runners\RunnerBase.vb 66 39 IMS
我有点不知所措.我不知道为什么它不能弄清楚要调用哪个函数.有什么想法吗?
I am kind of at a loss here. I am not sure why it cant figure out which function to call. Any ideas?
推荐答案
您必须将obj
指定为SIMType
而不是DAOBase
:
You have to specify obj
as SIMType
instead of DAOBase
:
Public Function Modify(Of SIMType As {New, DAOBase})(ByVal obj As SIMType) As Boolean
否则,您的通用类型参数将毫无用处.
Otherwise your generic type parameter would be useless.
您的DAOToGP函数具有不同的签名,并且显然不是从基类派生的.试试这个:
Your DAOToGP function have different signatures and are apparently not derived from a base class. Try this:
Public Class DAOBase(Of Tin, Tout)
Public Function DAOToGP(ByVal obj As Tin) As Tout
End Function
End Class
Public Module Test_DAOBase
Public Function Modify(Of Tin, Tout)(ByVal obj As DAOBase(Of Tin, Tout)) As Boolean
End Function
End Module
您还可以将DAOBAse声明为抽象类(MustInherit),将DAOToGP声明为抽象函数(MustOverride):
You could also declare DAOBAse as an abstract class (MustInherit) and DAOToGP as an abstract function (MustOverride):
Public MustInherit Class DAOBase(Of Tin As {New}, Tout)
Public MustOverride Function DAOToGP(ByVal obj As Tin) As Tout
End Class
Public Class DAOProduct
Inherits DAOBase(Of Products, SalesItem)
Public Overrides Function DAOToGP(ByVal obj As Products) As SalesItem
Return Nothing
End Function
End Class
Public Module Test_DAOBase
Public Function Modify(Of Tin As {New}, Tout)(ByVal obj As DAOBase(Of Tin, Tout)) As Boolean
Dim out As Tout = obj.DAOToGP(New Tin()) 'This is OK
End Function
Public Sub TestModify()
Dim daoProd = New DAOProduct()
Modify(daoProd) 'This is OK
End Sub
End Module
然后为参数类型的不同组合(在我的示例中为DAOProduct
)声明从DAOBase
继承的不同类.
Then declare different classes inheriting from DAOBase
for the different combinations of parameter types (DAOProduct
in my example).
这篇关于处理泛型时VB.Net中的多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!