开发者

Actionscript 3 shareable functions (interfaces?)

I've been working on OOP methods lately, and here is something I've always been wondering.

Let say we have the followin开发者_运维技巧g situation. We have a collection of pets [dog, cat, mouse]. Each pet has the same behaviour: running, eating, sleeping. Furthermore, they all have a different visual representation.

Instead of making 3 seperated classes and recreate those 3 functions like this:

  • dog
  • eat function
  • run function
  • sleep function

[same goes for cat and mouse]

Is there a better way to simply "share" a collection of functions between these different classes, and somehow differ the behaviour of these function depening on the chosen class (the walking speed of a dog might be quicker than a cat, a cat might sleep more than a mouse etc).

I've looked at interfaces, but as far as I understood you can't actually code the functions, the interface just acts of a list of requirements that a class should have. Could be very wrong tho. Help is much appreciated :)


Sounds like you just need inheritance?

class Animal {
    public function eat():void { stuff }
    public function run():void { stuff }
    public function sleep():void { stuff }
}

class Dog extends Animal {
}

...

I use this sort of thing all over my games.

To differ depending on the subclass:

If it's a big change, then obviously you should override the method in the subclass.

class Dog {
    public function wagTail():void { stuff }
    override public function run():void {
        super.run();
        wagTail();
    }
}

If it's a small change, you might choose to give the behaviour to the parent class and use member variables to alter it slightly:

class Animal {
    protected var walkSpeed:Number;
    public function walk():void {
        x += walkSpeed;
    }
}
class Dog {
    public function Dog() {
        walkSpeed = 6;
    }
}

Personally I never found interfaces terribly useful in AS3 -- every time I tried using them I ended up refactoring them later into classes instead, when I realised I wanted to share functionality between the implementations.


I think interfaces serves more benefits for the purpose of using polymorphism. Say Animal interface

public interface Animal{
     function eat();
}

public class DogA implements Dog implements Animal{
   public eat(){ ... }
}

public class CatA implements Cat implements Animal{
   public eat(){ ... }
}

public class Main{

   public function getAnimal(a1:Animal){  // so you can pass in different animals
       if(a1 is Dog){
          ... do this
       }else if(a1 is Cat){
          ... do this
       }
   }

}

but it's something in this sense is why interface is sometimes better


As3 supports interfaces, try creating an interface for a mammal

package com.you.interfaces
{
public interface Mammal
{

    //--------------------------------------
    //  PUBLIC METHODS
    //--------------------------------------
    function eat():void;
    function move(speed:uint = 0):void;
    function sleep():void;

    //--------------------------------------
    //  GETTER/SETTERS
    //--------------------------------------
    function get health():uint;
    function set health(value:uint):void;
}
}

In a mammal implementation:

package com.you.animals
{
import com.philipbroadway.interfaces.Mammal;
import flash.display.Sprite;

public class Dog extends Sprite implements Mammal
{
    //--------------------------------------
    //  CONSTRUCTOR
    //--------------------------------------
    public function Dog()
    {
        super();
    }

    //--------------------------------------
    //  PRIVATE VARIABLES
    //--------------------------------------
    private var _hp:uint;
            private var _moveSpeed:uint;

    //--------------------------------------
    //  GETTER/SETTERS
    //--------------------------------------
    public function get health():uint
    {
        return 0;
    }

    public function set health(value:uint):void
    {
                   _hp = value;
    }
    //--------------------------------------
    //  PUBLIC METHODS
    //--------------------------------------
    public function eat():void
    {
                health++;
    }

    public function move(speed:uint=0):void
    {
                _moveSpeed++;
    }

    public function sleep():void
    {
                health++;
    }

}

}

Any of your other animals simply implement the Mammal class and all its methods. If you keep them abstract enough they should work across more animals. This allows you to manipulate a large assortment of animals knowing they all have a set of common methods expecting the same arguments, without creating an inheritance nightmare by just subclassing.

hope this helps. -philip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜