How to avoid reusing identical implementation code with an interface?

前端 未结 2 544
日久生厌
日久生厌 2021-01-06 06:04

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 u

相关标签:
2条回答
  • 2021-01-06 06:40

    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

    0 讨论(0)
  • 2021-01-06 06:55

    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...

    0 讨论(0)
提交回复
热议问题