Class结构教程
1. 最基本的Class定义方面我不多说了, 就举个例子直接开始:class Flash8 {
private var user:String; //定义变量
private var pass:String;
private var age:Number;
public function Flash8(x:String, y:String) { //class的结构函数
user = x;
pass = y;
trace("用户名 = " user ", 密码 = " pass);
}
public function setAge(x:Number):Void { //class中的定义函数
age = x;
}
public function getAge() {
trace(user " is " age " years old.");
}
}
--------------- 在 Flash 的代码 ------------
var a = new Flash8("Super-Tomato", "123456");
a.setAge(24);
a.getAge(); //得到的输出结果是:
//用户名 = Super-Tomato, 密码 = 123456
//Super-Tomato is 24 years old. 2. 再来是Inheritance继承并且使用Overriding, 引用之前的Flash8类,作为SuperClass class Flash8 {
private var user:String; //定义变量
private var pass:String;
private var age:Number;
public function Flash8(x:String, y:String) { //class的结构函数
user = x;
pass = y;
trace("用户名 = " user ", 密码 = " pass);
}
public function setAge(x:Number):Void { //class中的定义函数
age = x;
}
public function getAge() {
trace(user " is " age " years old.");
}
}
-------------- Board1类 -------------------
class Board1 extends Flash8 { //继承都必定要使用extends来指定所继承的SuperClass
public function Board1() {
trace("这个是继承类SubClass, SuperClass还是会被执行");
}
public function getAge() {
trace("这里使用了Overriding");
}
}
--------------- 在Flash的代码 --------------
var a = new Board1();
a.getAge(); //输出的结果为 :
//用户名 = undefined, 密码 = undefined
//这个是继承类SubClass, SuperClass还是会被执行
//这里使用了Overwritten 从这里可以知道, Flash8类还是有本执行.....当中的继承之间都同时出现了getAge()函数, 到底要用哪个getAge()才是对的呢??
答案是Board1的getAge(), 原因是因为他已经derived(继承)了Flash8, 也就是说Flash8的getAge()被Override(可以说被取代)了 AOL之前也有写过这样的例子:
http://www.flash8.net/teach.aspx?id=1435
http://www.flash8.net/teach.aspx?id=599
3. Polymorphism (多形) 其实Polymorphism和Inheritance一样是被override, 但polymorphism则是多个继承, 接下来的例子当中我会使用到super和调用SuperClass的方法:
class Flash8 {
private var user:String;
private var pass:String;
private var age:Number;
private var str:String;
public function Flash8(x:String, y:String) {
user = x;
pass = y;
trace("用户名 = " user ", 密码 = " pass);
}
public function setAge(x:Number):Void {
age = x;
}
public function getAge() {
str = user " is " age " years old.";
return str;
}
}
----------- Board1 类 -------------------
class Board1 extends Flash8 {
public function Board1(a:String, b:String) {
super(a, b);
}
public function getString() {
str = "In Board1 " getAge();
trace(str);
}
}
----------- Board2 类 -------------------
class Board2 extends Flash8 {
private var str:String;
pub
页:
[1]