如何避免在接口中重用相同的实现代码? [英] How to avoid reusing identical implementation code with an interface?

查看:16
本文介绍了如何避免在接口中重用相同的实现代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为又一个界面问题"表示歉意.不过,我认为这个问题可能值得一问,因为这是一个奇怪的问题.我正在使用的项目使用 Actionscript 3,但这更像是一个通用的 OOP 问题.

First of all, I apologize for "yet another interface question". I thought that this one might be worth asking, though, as it's kind of a strange issue. The project I'm using uses Actionscript 3, but this is more of a general OOP question.

情况如下:

我有一个已经从基类继承的类;它是电子游戏中的一个对象.(假设它是一艘宇宙飞船.)在我的游戏中,我希望一次在屏幕上显示许多许多宇宙飞船,因此我决定使用链表结构创建一个对象池.

I have a class that already inherits from a base class; it's an object in a videogame. (Let's say it's a spaceship.) In my game, I'd like to have many many many spaceships onscreen, at one time, so I've decided to create an object pool using a linked-list structure.

逻辑上,因为类 Spaceship 已经继承自一个基类,所以我会使用一个接口来定义与链表相关的方法.此外,这样做可以让我将这些方法扩展到其他类 - 例如 Asteroid 类、Bullet 类或 Particle 类.

Logically, because class Spaceship already inherits from a base class, I would use an interface to define methods pertaining to a linked list. Doing this, additionally, would allow me to extend these methods to other classes - class Asteroid, or class Bullet, or class Particle, for instance.

问题来了 - 接口的优势在于它允许您根据需要重新定义使用的每个类的实现.但是,对于链表之类的东西,代码不会在类之间改变.因为我计划有很多类来实现这些对象池方法,所以我真的很想避免在每个新类中一遍又一遍地重用相同的实现代码.

Here's the problem - the strength of an interface is that it lets you redefine the implementation for every class you use, based on what you need. For something like a linked list, though, the code isn't going to change between classes. Since I plan on having a lot of classes that will implement these object pool methods, I'd really like to avoid reusing the same implementation code over and over within each new class.

问题:有没有办法避免为我使用的每个类重复使用完全相同的链表代码?或者这只是一种必然?这似乎违反了一次且仅一次的原则.是否可以在继承块中定义一个完整的函数,而不仅仅是它的原型?

Question: Is there a way to avoid reusing the same exact linked list code for each class I use? Or is this just an inevitability? It seems like this violates the Once and Only Once principle. Is it possible to define a full function within an inheritance block, instead of just its prototype?

如果这是一个愚蠢的问题,请告诉我.(如果我问它是否是,它可能是,但是嘿,如果不犯傻就不能学习.)似乎应该有一种更合乎逻辑的方法来做这样的事情.

If this is a stupid question, let me know. (If I'm asking whether it is, it probably is, but hey, can't learn without being stupid once in a while.) It just seems that there should be a more logical way to do something like this.

推荐答案

在其他支持多重继承的语言中,有一种简单的方法可以做到这一点,而您的所有游戏对象都不需要有一个共同的祖先:您的飞船可以继承来自基础船类以及链表元素类.不幸的是,AS3 不支持多重继承,因此您必须选择其中之一:

In other languages, that support multiple inheritance, there is an easy way to do it without all your game objects needing to have a common ancestor: your spaceships could inherit from a base ship class, as well as a linked list element class. Unfortunately, AS3 does not support multiple inheritance, so you have to choose to either:

A.完全按照您的建议使用界面

A. Use an interface exactly as you suggest

B.让您的所有游戏对象基类都继承自一个共同的祖先,该祖先实现了链表功能并扩展了 Sprite(或 MovieClip),而不是直接扩展 Sprite 或 MovieClip 的游戏对象基类.

B. Have all your game object base classes inherit from a common ancestor that implements the linked list functionality and extends Sprite (or MovieClip), instead of your game object base classes extending Sprite or MovieClip directly.

我可能会选择后者,以解决您的确切问题.我可能会从 Sprite 或 MovieClip 派生并调用 GamePoolObject 或类似的东西......

I'd probably go with the latter, to solve your exact problem. I'd probably derive from Sprite or MovieClip and call the thing GamePoolObject or something to that effect...

这篇关于如何避免在接口中重用相同的实现代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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