.net 是否有类似于只声明函数命名而不声明参数和/或返回类型的接口?
我只是从接口开始,我想知道是否有一个概念来处理我遇到的一种情况,我想用返回实现接口的类类型的函数实现一个公共接口.net 是否有类似于只声明函数命名而不声明参数和/或返回类型的接口?,.net,vb.net,.net,Vb.net,我只是从接口开始,我想知道是否有一个概念来处理我遇到的一种情况,我想用返回实现接口的类类型的函数实现一个公共接口 Public Interface IFoo Function GiveMeSomething() As Object End Interface Public Interface IFoo(Of T) Inherits IFoo Overloads Function GiveMeSomething() As T End Interface Publ
Public Interface IFoo
Function GiveMeSomething() As Object
End Interface
Public Interface IFoo(Of T)
Inherits IFoo
Overloads Function GiveMeSomething() As T
End Interface
Public Class Foo
Implements IFoo(Of Foo)
Public Function GiveMeANewFoo() As Foo Implements IFoo(Of Foo).GiveMeSomething
Return New Foo()
End Function
Private Function GiveMeANewFooInternal() As Object Implements IFoo.GiveMeSomething
Return Me.GiveMeANewFoo()
End Function
End Class
是否有这样一种功能或概念上的方法来实现这一点?每次调用都不传入类型
从概念上讲,是指
Interface IFoo
Function GiveMeSomething(of T)() As T
End Interface
Public Class MyFoo
Implements IFoo
Public Function GiveMeANewFoo(Of T)() As T Implements IFoo.GiveMeSomething
return new T
End Function
End Class
然后是用法
Dim oldFoo as new MyFoo()
Dim newFoo As MyFoo = oldFoo.GiveMeANewFoo()
你需要的是一份工作
另一种常用的设计模式是让通用接口继承“基本接口”
非常感谢。正是我需要的:)
Dim oldFoo As New Foo()
Dim newFoo As Foo = oldFoo.GiveMeANewFoo()
Public Interface IFoo
Function GiveMeSomething() As Object
End Interface
Public Interface IFoo(Of T)
Inherits IFoo
Overloads Function GiveMeSomething() As T
End Interface
Public Class Foo
Implements IFoo(Of Foo)
Public Function GiveMeANewFoo() As Foo Implements IFoo(Of Foo).GiveMeSomething
Return New Foo()
End Function
Private Function GiveMeANewFooInternal() As Object Implements IFoo.GiveMeSomething
Return Me.GiveMeANewFoo()
End Function
End Class
Dim oldFoo As Foo = New Foo()
Dim newFoo As Foo = oldFoo.GiveMeANewFoo()
' IFoo(Of Foo).GiveMeSomething | Foo.GiveMeANewFoo
Dim oldFoo As IFoo(Of Foo) = New Foo()
Dim newFoo As Foo = oldFoo.GiveMeSomething()
' IFoo(Of Foo).GiveMeSomething | Foo.GiveMeANewFoo
Dim oldFoo As IFoo = New Foo()
Dim newFoo As Object = oldFoo.GiveMeSomething()
' IFoo.GiveMeSomething | Foo.GiveMeANewFooInternal