AS3:如何需要方法参数来实现多个接口?

发布于 2024-08-31 05:05:33 字数 1028 浏览 3 评论 0原文

我的方法 func() 的参数必须实现两个不相关的接口 IFoo 和 IBar。有没有比仅出于此目的声明另一个继承自 IFoo 和 IBar 的接口并使用该接口作为参数类型更好的方法?

public interface IFooBar extends IFoo, IBar {
}

public function func(arg:IFooBar):void {
}

我正在为 Flash Player 9 进行开发。

我正在尝试执行的具体操作:

我正在编写一个小型类库,旨在帮助我开发 Flash 游戏。为了帮助调试和验证游戏结果,该库要求用户将游戏拆分为以下组件:

  • rawInputProducer:从 Flash 输入事件、上一个会话中保存的事件或某些其他源生成设备输入事件
  • rawInputProcessor:侦听设备输入并从中生成用户命令
  • gameController:侦听用户命令并更新游戏状态
  • gameView:侦听来自 gameController 的游戏状态更改并更新舞台

所有这些组件都会生成事件(称为“消息”,以免与 flash 事件混淆) 。 Engine 类启动组件并运行主循环。

为了使组件尽可能通用,Engine 仅要求它们实现 IMessageDispatcherITickable。我想编写仅是 IMessageDispatcher 或仅 ITickable 的类。我还有一个通用的 MessageDispatcher ,它实现了我的组件可能会扩展的 IMessageDispatcher

最初的问题是关于如何声明如下方法:

Engine.setGameController(controller: ITickable & IMessageDispatcher):void

The argument to my method func() must implement the two unrelated interfaces IFoo and IBar. Is there a better way of doing this than declaring another interface only for this purpose that inherits from IFoo and IBar and using that interface as the argument type?

public interface IFooBar extends IFoo, IBar {
}

public function func(arg:IFooBar):void {
}

I'm developing for Flash Player 9.

Specifics of what I'm trying to do:

I'm writing a small library of classes that are intended to help me in the developing Flash games. To help in debugging and verification of game results the library requires that the user splits the game into the following components:

  • rawInputProducer: Produces device input events from either flash input events, saved events from a previous session, or some other source
  • rawInputProcessor: Listens to device input and from them produces user commands
  • gameController: Listens to user commands and updates the game state
  • gameView: Listens to game state changes from gameController and updates the stage

All these components generate events (called 'messages' to not confuse them with flash events). The class Engine initiates the components and runs the main loop.

In order to allow for components to be as generic as possible Engine only requires that they implement IMessageDispatcher and ITickable. I will want to write classes that are only a IMessageDispatcher or only ITickable. I also have a generic MessageDispatcher that implements IMessageDispatcher that my components probably will extend.

The original question regards how I can declare a method like the following:

Engine.setGameController(controller: ITickable & IMessageDispatcher):void

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-09-07 05:05:33

声明一个扩展两个(或所有)所需接口的接口是正确的方法。我想不出更好的办法了。

Declaring an interface that extends both (or all) of the required interfaces is the correct way to do it. I can't think of a better way.

甜警司 2024-09-07 05:05:33

让一个接口扩展 2 个不相关的接口,这样你就可以“正确”输入一个函数/方法的参数,这似乎是一种代码味道。你可以侥幸逃脱,但我认为这可能是设计问题的一个症状。

不过,我可能是错的,因为我对你的具体问题一无所知。我假设 IFoo 和 IBar 都是不相关的,但事实可能并非如此。

也许如果您更多地描述您的问题,这些接口代表什么以及所讨论的方法如何处理实现这两个接口的对象,其他人可能会建议您的设计的替代方案(如果碰巧有必要的话)。但是,作为一般经验法则,如果您这样做只是为了让编译器满意,那么您可能会做错什么。

Having an interface extend 2 unrelated interfaces just so you can "correctly" type the parameter of one function / method seems like a code smell. You can get away with it, but I think it might be a symptom a design problem.

I might be wrong, though, as I don't know anything about your particular problem. And I'm assuming both IFoo and IBar are unrelated, which might not be the case.

Perhaps if you describe a bit more your problem, what these interfaces represent and how the method in question deals with the object that implements both interfaces, other people could suggest alternatives to your design (if that happens to be necessary). But, as a general rule of thumb, if you're doing it just to keep the compiler happy, there's a chance you're doing something wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文