1

我在继承方面遇到了麻烦,因为我从未在 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;
        }
    }
}

为什么我不能像那样让班级“猫”?有人请解释我如何继承一个类并仍然满足它的参数。我迷路了。谢谢。

4

2 回答 2

1

在你的 cat 类中,替换:

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

super(a, b, c);

this 调用基类/超类的构造函数,传入 a,b,c。

于 2013-01-28T03:08:41.493 回答
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/

于 2013-01-28T03:10:24.657 回答