在基类和事件之间的冲突在派生类中实现接口事件 [英] Conflict between event in base class and implementation of interface event in derived class

查看:128
本文介绍了在基类和事件之间的冲突在派生类中实现接口事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基类:

Public MustInherit Class Connector

    Public Event Changed as EventHandler

End Class

接口:

Public Interface IPlug

    Event Changed as EventHandler

End Interface

派生类:

Public Class OutputConnector
    Inherits Connector
    Implements IPlug

    Event Changed as EventHandler Implements IPlug.Changed

End Class

VB.Net问题:

VB.Net Problem:

更改事件 OutputConnector 更改事件连接,当然。但我要实现它以满足 IPlug 接口。

The Changed event in OutputConnector conflicts with the Changed event in Connector, of course. But I have to implement it to satisfy the IPlug interface.

我该如何解决这个问题?

How can I solve this?

我有其他的类连接器导出(例如,一个 InputConnector 类)需要有更改事件,但不执行 IPlug 。我不希望出现这种情况正在实施的派生类 IPlug 有一个额外的活动只是为了避免名称冲突(这将是情况下,如果我改变了事件的名称无论是在基类或接口)。

I have other classes deriving from Connector (e.g. an InputConnector class) that need to have the Changed event, but NOT implement IPlug. And I don't want derived classes that are implementing IPlug to have an additional event just to avoid the name conflict (which would be the case if I changed the name of the event in either the base class or the interface).

有什么建议?

编辑:

问题不在于名称本身,而是我想要的派生类的接口事件实现某种方式映射到基类的现有事件 - 而不必声明在派生类中的其他事件(与所有各种路由杂波启动)的;毕竟,该事件已经存在于基类!我需要的是一个方法,让接口知道..

The problem is not the names per se, but that I want the derived class to somehow map its interface event implementation to the existing event in the base class - and not have to declare an additional event in the derived class (with all sorts of routing clutter to boot); after all, the event already exists in the base class! What I need is a way to let the interface know that..

一个伪溶液(只是为了说明)将是能够把东西的效果

A pseudo-solution (just to illustrate) would be to be able to put something to the effect of

Public Event MyBase.Changed Implements IPlug.Changed

在派生的 OutputConnector 类。

推荐答案

这个问题指出两种方法可以使一个接口实现明确的。

This question points out two ways to make an interface implementation explicit.

  • 请其私人
  • 使用的事实,实现的名称不必与接口名称。

所以,这取决于你如何计划使用 IPlug.Changed 。如果你总是会使用接口进行交互,那么你可以简单地把它变成私有的,即使它要求阴影通过公共API,它是不可见的。

So it depends on how you plan on using IPlug.Changed. If you will always be using the interface to interact, then you can simply make it private, and even if it requires Shadows it won't be visible via the public API.

另一种方法是决定一个新的名称申报,如 OutputConnector.IPlugChanged

The other way is to decide a new name for the declaration, such as OutputConnector.IPlugChanged.

您选择取决于您所使用的一切。

Which you select depends on how you are using everything.

编辑:

您似乎想这两个事件是在功能上是相同的。

You seem to want the two events to be identical in functionality.

在这种情况下,做的最好的事情是通过使私人用不同的名称来隐藏 IPlug.Changed 的实施。

In that case the best thing to do would be to hide the IPlug.Changed implementation by making it private with a different name.

一旦你隐藏 IPlug.Changed ,使用的OnChange 受保护的方法(假设你做了一个)链接在两个,叫 IPlugChanged 的OnChange 被触发。

Once you have hidden IPlug.Changed, use your OnChange protected method (assuming you made one) to link up the two, calling IPlugChanged when OnChange is triggered.

如果你没有一个的OnChange 受保护的方法,可以将执行上面的电话,而不是一个事件。请注意,如果你担心顺序事件称为,这是不行的。

If you don't have an OnChange protected method, you can attach an event that performs the above call instead. Note that if you are worried about the order events are called in, this won't work.

最后一个选择可能是使用自定义语法流修改 IPlug 的基类。

A final option may be to use the Custom syntax to stream changes to IPlug to the base class.

这篇关于在基类和事件之间的冲突在派生类中实现接口事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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