如何避免在接口中重复使用相同的实现代码?
首先,我为“又一个界面问题”道歉。不过,我认为这个问题可能值得一问,因为这是一个奇怪的问题。我正在使用的项目使用 Actionscript 3,但这更多是一个一般的 OOP 问题。
情况是这样的:
我有一个已经从基类继承的类;它是电子游戏中的一个物体。 (假设它是一艘宇宙飞船。)在我的游戏中,我希望屏幕上同时出现很多很多宇宙飞船,因此我决定使用链表结构创建一个对象池。
从逻辑上讲,因为 Spaceship 类已经继承自基类,所以我将使用接口来定义与链表相关的方法。此外,这样做还可以让我将这些方法扩展到其他类 - 例如 Asteroid 类、Bullet 类或 Particle 类。
问题是 - 接口的优势在于它可以让您根据需要重新定义所使用的每个类的实现。不过,对于像链表这样的东西,代码在类之间不会改变。由于我计划有很多类来实现这些对象池方法,因此我真的很想避免在每个新类中一遍又一遍地重复使用相同的实现代码。
问题:有没有办法避免为我使用的每个类重复使用完全相同的链表代码?或者这只是一种必然?这似乎违反了“一次且仅一次”原则。是否可以在继承块中定义完整的函数,而不仅仅是其原型?
如果这是一个愚蠢的问题,请告诉我。 (如果我问是否是,那么可能是,但是嘿,如果不偶尔犯傻就无法学习。) 似乎应该有一种更合乎逻辑的方法来做这样的事情。
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.
Here's the situation:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在支持多重继承的其他语言中,有一种简单的方法可以做到这一点,而无需所有游戏对象都需要有一个共同的祖先:您的宇宙飞船可以从基本船舶类以及链表元素类继承。不幸的是,AS3 不支持多重继承,因此您必须选择:
A. 完全按照您的建议使用接口
,或者
B. 让所有游戏对象基类继承自实现链表功能并扩展 Sprite 的共同祖先(或 MovieClip),而不是直接扩展 Sprite 或 MovieClip 的游戏对象基类。
我可能会选择后者,以解决您的具体问题。我可能会从 Sprite 或 MovieClip 派生并调用 GamePoolObject 或类似的东西......
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. Use an interface exactly as you suggest
or
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.
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...
我从来不喜欢使用界面。
我要做的是创建另一个基类
第一个类将是基类,我们将其称为 SpaceObject。所有空间物体都具有质量、速度、摩擦(如果有)、旋转属性。 SpaceObject 还将扩展 UIComponent,因为我们希望最终将其添加到显示列表中,并且继承用于命中检测的方法会很好。
它还将具有处理运动以及空间物体所需的任何其他内容的方法。
然后,每当我想要创建一个新的空间对象时,我只需扩展 SpaceObject 类即可。
公共类 SpaceShip 扩展 SpaceObject 或
公共类 Commet 扩展了 SpaceObject。
这些类将继承 SpaceObjects 属性,并拥有自己的属性,例如尾巴的长度或它拥有的武器。应该封装它们以处理与它们相关的任何内容
I never liked using interfaces.
What I would do is make another base class
The first class will be the base class lets call it SpaceObject. All space objects have attributes mass,velocity,friction(if any), rotation. SpaceObject will also extend UIcomponent as we want this to be added to the display list eventually and having inherited methods for hit detection would be nice.
It will also have methods that will handle movement and whatever else needed for space objects.
Then anytime I want to make a new space object I would just extend the SpaceObject class.
public class SpaceShip extends SpaceObject or
public class Commet extends SpaceObject.
These classes will inherit the SpaceObjects attributes plus have their own like how long the tail is or what weapon it has. They should be encapsulated to handle whatever relates to them