接口继承帮助 [英] Interface Inheritence Help

查看:82
本文介绍了接口继承帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助 - 我可能做错了,所以请更正并且

建议!


我正在学习MVP方法,我似乎掌握得很好。现在

我正在尝试更高级的东西。我在

界面中声明了一个事件。我正在通过另一个继承的界面消费界面。


我想让我的课程回应这个''one''事件...


根据我的下面的示例代码,我希望将类Test1和Test2都用于

当它被引发时响应EventToShare。我该怎么做呢?我有以下设置

的东西...


示例代码:


Public Interface IView_Base


活动EventToShare(SomeText as String)


结束界面


公共界面IView_Test1

继承IView_Base


事件AnotherEvent


结束界面


公共接口IView_Test2

继承IView_Base


事件AnotherEvent2

结束界面


公共类Test1


dim myObj为IView_Test1


Public Sub New(查看为IView_Test1)

myObj =查看

addhandler myObj.EventToShare,AddressOf Sub1

End Sub


Private Sub Sub1(文字为字符串)

''做带文字的东西

结束子

结束班


公共类测试2


dim myObj为IView_Test2


Public Sub New(查看为IView_Test2)

myObj =查看

addhandler myObj.EventToShare,AddressOf Sub2

End Sub


Private Sub Sub2(文字为字符串)

''用这里的文字做其他事情

结束子

结束课

感谢您的帮助!


Wayne P.

解决方案

Wayne,

您使用的技术不是接口,而是继承。要实现您的类作为接口,请执行以下操作。


公共接口IView_Base

事件EventToShare(SomeText as String)

结束界面

公共接口IView_Test1

实现IView_Base

事件AnotherEvent

结束接口

公共接口IView_Test2

实现IView_Base

事件AnotherEvent2

结束接口


如果你看起来是我的上一篇文章,请忽略它。


好​​的,你需要创建一个实现你的接口的对象。


公共类CustomObject

实现IView_Test2


.....代码将自动创建以处理事件


结束班级


公共类测试2

将myObj调整为自定义对象

Public Sub New(查看为IView_Test2)

myObj =查看

addhandler myObj.EventToShare,AddressOf Sub2

End Sub

Private Sub Sub2(文字为字符串)

''在此处用文字做其他事情

End Sub

结束类


当您在类中实现接口时,您的设计器窗口

将创建事件的正确链接。


公共类CustomObject

实现IView_Test2


公共事件AnotherEvent()实现iView_Test1 .AnotherEvent

公共事件AnotherEvent2()实现iView_Test2.AnotherEvent2


结束课


在课堂上使用它像这样......

公共类测试2

私人myObj为IView_Test2

公共子新(查看为IView_Test2)

myObj =查看

addhandler myObj.EventToShare,AddressOf Sub2

End Sub

Private Sub Sub2(Text as String)

''用这里的文字做其他事情

结束次级

结束班

Need some help - and I may be doing this wrong, so please correct and
suggest!

I''m learning the MVP method, which I seem to have a good grasp of. Now
I am trying something a bit more advanced. I''m declaring an event in a
interface. I''m consuming the interface via another inherited Interface.

I''d like to have my classes respond to this ''one'' event...

Based on my below example code, I''d like both Classes Test1 and Test2 to
Respond to EventToShare when it gets raised. How do I do this? I have
things setup as below...

Example Code:

Public Interface IView_Base

Event EventToShare(SomeText as String)

End Interface

Public Interface IView_Test1
Inherits IView_Base

Event AnotherEvent

End Interface

Public Interface IView_Test2
Inherits IView_Base

Event AnotherEvent2
End Interface

Public Class Test1

dim myObj as IView_Test1

Public Sub New(View as IView_Test1)
myObj = View
addhandler myObj.EventToShare, AddressOf Sub1
End Sub

Private Sub Sub1(Text as String)
''Do something with the text
End Sub
End Class

Public Class Test2

dim myObj as IView_Test2

Public Sub New(View as IView_Test2)
myObj = View
addhandler myObj.EventToShare, AddressOf Sub2
End Sub

Private Sub Sub2(Text as String)
''Do something else with the text over here
End Sub
End Class
Thanks for the help!

Wayne P.

解决方案

Wayne,
The technique you are using is not interfaces, but inheritance. To
implement your classes as interfaces do the following.

Public Interface IView_Base
Event EventToShare(SomeText as String)
End Interface
Public Interface IView_Test1
Implements IView_Base
Event AnotherEvent
End Interface
Public Interface IView_Test2
Implements IView_Base
Event AnotherEvent2
End Interface


If you seem my last post, ignore that.

Ok, you need to create an object that will Implement your interfaces.

Public Class CustomObject
Implements IView_Test2

.....code will be created automagically to handle events

End Class

Public Class Test2
dim myObj as CustomObject
Public Sub New(View as IView_Test2)
myObj = View
addhandler myObj.EventToShare, AddressOf Sub2
End Sub
Private Sub Sub2(Text as String)
''Do something else with the text over here
End Sub
End Class


When you implement an interface into a class, your designer window
will create the proper links for the events.

Public Class CustomObject
Implements IView_Test2

Public Event AnotherEvent() Implements iView_Test1.AnotherEvent
Public Event AnotherEvent2() Implements iView_Test2.AnotherEvent2

End Class

Use it in your class like so...
Public Class Test2
private myObj as IView_Test2
Public Sub New(View as IView_Test2)
myObj = View
addhandler myObj.EventToShare, AddressOf Sub2
End Sub
Private Sub Sub2(Text as String)
''Do something else with the text over here
End Sub
End Class


这篇关于接口继承帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆