[英]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.