1.通过这段程序可以看出类可以直接调用类里面的静态属性和静态方法,
而通过类创建的实例则不能访问类的静态属性和静态方法。
package father{
public class TestStatic{
public static var username="adreamstar";
public function TestStatic(){
};
public static function test():void{
trace("TestStatic is used");
}
}
}
文档类:
package{
import father.TestStatic;
import flash.display.Sprite;
public class Test extends Sprite{
public function Test(){
var testStatic:TestStatic=new TestStatic();
trace(TestStatic.username);
TestStatic.test(); //输出成功
//trace(testStatic.username); 输出失败
//testStatic.test();
}
}
}
2.通过继承的子类也可以调用父类的静态方法。
package father{
public class TestStatic{
public static var username="adreamstar";
public function TestStatic(){
};
public static function test():void{
trace("TestStatic is used");
}
}
}
package son{
import father.TestStatic;
public class StaticInheri extends TestStatic{
public function StaticInheri(){
super();
}
public function testson():void{
trace(username);
test();
}
}
}
文档类:
package{
import father.TestStatic;
import son.StaticInheri;
import flash.display.Sprite;
public class Test extends Sprite{
public function Test(){
var staticInheri:StaticInheri=new StaticInheri();
staticInheri.testson();
}
}
}