Understanding AS3 super() statement
2009-10-18 00:00:00 来源:WEB开发网One interesting statement that’s almost unexplained in the web is super().
Before explaining super()’s features, let me talk about superclasses.
From Wikipedia: In computer science, a superclass is a class from which other classes are derived. A superclass is also called a parent class’. The classes that are derived from a superclass are known as child classes, derived classes, or subclasses. We can also say that a class A extends class B when A is a subclass of the B superclass.
Ok, now we know a superclass provides data (instance variables) and functions (methdods) to be inherited by the subclass.
So what can you do with super() statement?
With this constructor you can call the superclass or methods of the superclass (superclass’ functions) by just using the super prefix as a reference.
Let’s take this class:
package {
import flash.display.Sprite;
public class real_class extends Sprite {
public function real_class(s:String) {
trace("I am the text contained in the real class");
trace(s);
}
public function another_function() {
trace("I am the text passed in another function");
}
}
}
This is our superclass, and it’s not the main class inside our Flash project.
The main class is this one:
package {
import flash.display.Sprite;
public class super_example extends real_class {
public function super_example() {
trace("I am the text contained in the main class");
super("I am the text passed in the real class");
super.another_function();
}
}
}
As you can see, at line 3 I am not extending the Sprite class as usual, but the real_class one.
So in this case I could remove the import flash.display.Sprite; line.
At line 5 I am tracing some text, then at line 6 I am calling the superclass passing another text and at line 7 I am calling a metod, that is a function in the real_class you can find at line 8 that outputs more text.
The result is this one:
I am the text contained in the main class
I am the text contained in the real class
I am the text passed in the real class
I am the text passed in another function
Using superclasses will improve reusability, it means you are writing code that can be used again to add new functionalities with slight or no modification
Tags:Understanding AS super
编辑录入:爽爽 [复制链接] [打 印]- ››asp.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
更多精彩
赞助商链接