AS3继承和参数问题

时间:2013-01-28 02:50:24

标签: actionscript-3 class inheritance parameters

我在继承方面遇到了麻烦,因为我在ActionScript 3中从未这样做过。

请告诉我在这种情况下该怎么做?

假设我有以下课程

package
{
    public class animal
    {
        var age;
        var amountOfLegs;
        var color;
        public function animal(a,b,c)
        {
            age=a;
            amountOfLegs=b;
            color=c;
        }
    }
 }

然后,我想制作派生类

package
{
    public class cat extends animal
    {
        var hairType;
        public function cat(a,b,c,d)
        {
            age=a;
            amountOfLegs=b;
            color=c;
            hairType=d;
        }
    }
}

为什么我不能像这样制作班级'猫'? 有人请解释我如何继承一个类并仍然满足其参数。 我迷路了。 感谢。

2 个答案:

答案 0 :(得分:1)

在你的cat类中,替换:

age=a;
amountOfLegs=b;
color=c;

super(a, b, c);

这会调用base / super类的构造函数,传入a,b,c。

答案 1 :(得分:0)

您需要使用super来调用父类构造函数并传入您的值。

http://www.emanueleferonato.com/2009/08/10/understanding-as3-super-statement/

考虑这个例子

//this class defines the properties of all Animals

public class Animal{

    private var _age:int;
    private var _amountOfLegs:int;
    private var _color:String;

    public function Animal(age:int, amountOfLegs:int, color:String){
         _age = age;
         _amountOfLegs = amountOfLegs;
         _color = color;
    }


    public function traceMe():void{

         trace("age: " + _age + "legs: " + _amountOfLegs + " color: " + _color);
    }

}

//this makes a cat
public class Cat extends Animal{
   public function Cat(){
        //going to call the super classes constructor and pass in the details that make a cat
        super(5, 4, "black");
        traceMe(); //will print age: 5 legs: 4 color: black
   }

}

更多阅读:

http://active.tutsplus.com/tutorials/actionscript/as3-101-oop-introduction-basix/

相关问题