ActionScript 3 继承时静态变量和函数的使用
类型共享(static)属性和方法是不能被子类所继承,就是说静态变量和静态函数都是不能被继承的。
ActionScript 3允许对象类型定义与类型共享型属性和方法同名的私有型或方法。就是说可以定义静态变量和非静态变量,而且命名可以相同,函数也是类似的
举例子test目录下创建SimpleObject.as
package test
{
public class SimpleObject
{
public static var member1:String = "Panda";
public static function testMethod1():String
{
return "static testMethod1";
}
public var member1:String = "Tiger";
public function testMethod1():String
{
return "instance testMethod1";
}
}
}
在创建MeanObject.as继承SimpleObject
package test
{
public class MeanObject extends SimpleObject
{
public function runTest():String
{
var output:String = "*** Static Property and Method Output:\n";
output += "SimpleObject.member1 = " + SimpleObject.member1; //调用静态变量member1输出Panda
output += "\n";
output += "SimpleObject.testMethod1() = " + SimpleObject.testMethod1();//调用静态函数testMethod1()输出static testMethod1
output += "\n";
output += "*** Instance Property and Method Output:\n";
output += "super.member1 = " + super.member1;//继承调用父类的变量member1
output += "\n";
output += "super.testMethod1() = " + super.testMethod1();//继承调用父类的函数testMethod1()
output += "\n";
return output;
}
}
}
flex中程序
viewSourceURL=""
horizontalAlign="center" verticalAlign="middle"
width="100%" height="100%"
>
import test.MeanObject;
private function showProperties():void
{
var obj1:MeanObject = new MeanObject();
panelPropertyArea.text = obj1.runTest();
}
]]>
paddingTop="5" paddingLeft="5" paddingRight="5" paddingBottom="5">
说明本文章是http://www.ajaxcn.net的作者读书笔记,书名《adobe flex3 程序设计指南》
结果如下:
*** Static Property and Method Output:
SimpleObject.member1 = Panda
SimpleObject.testMethod1() = static testMethod1
*** Instance Property and Method Output:
super.member1 = Tiger
super.testMethod1() = instance testMethod1
原创文章转载请注明出处:云飞扬IT的blog





