开发学院图形图像Flash Understanding AS3 super() statement 阅读

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

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

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接