[英]AS3 Inheritance and Parameters Issues
我在继承方面遇到麻烦,因为我从未在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;
}
}
}
为什么我不能只像这样使“猫”课? 有人请解释我如何可以继承一个类并仍然满足其参数。 我迷路了。 谢谢。
在您的Cat类中,替换为:
age=a;
amountOfLegs=b;
color=c;
同
super(a, b, c);
这将调用基类/超级类的构造函数,并传入a,b,c。
您需要使用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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.