Flash as learning: a summary of constructors

Source: Internet
Author: User
Tags abstract define constructor empty object object reference tostring
Function

  The difference of a constructor function

A constructor is a special member function that is common and differential with member functions. For example, a constructor, like a member function, is defined by a function keyword, can be declared private or public, and a parameter can be added to a constructor.
One, the difference of the constructor function mainly manifests in:
The L constructor name must be the same as the class name and is case-sensitive;
The L constructor cannot be declared static, it can only be private or public;
The L constructor cannot return a value using the returns statement;
The L constructor cannot return the specified data type;
When the above rules are violated, Flash prompts the appropriate information in the output panel.
1. In the following load class we define a member function load:

Class Load {
function Load () {
}
}

When you perform a grammar check, Flash prompts you with the following information:
The member function "load" is different from the name "load" of the defined class and will be treated as a class constructor at run time.
2, if we declare the static property in the constructor:

Class Load {
static function Load () {
}
}

Flash will prompt:
Properties allowed for constructors only public and private
3. If we use the return statement in the constructor, returns a value:

Class Load {
var i = 0;
function Load () {
return i;
}
}

Flash will prompt:
Constructor cannot return a value
4, if we return a data type in the constructor:

Class Load {
function load (): Load {
}
}

Flash will prompt:
Constructor cannot specify return type

  Second, the return value of the constructor

Although a value cannot be returned with return in a constructor, the constructor has a return value that returns an instance of the class.
Such as:

Class Load {
function Load () {
}
function toString (): String {
Return "instance of the Load class";
}
}

Test method:

Trace (New Load ());

Output in Output panel:
Instances of the Load class
The reason for adding the ToString method to the class:
When the object of the load class is treated as a string, the ToString () method is automatically invoked, which is useful when using trace () to debug the properties, instances, and so on of the class, so in classes that have constructors, the ToString () method is generally included, and the position is placed behind the constructor.
Without the ToString () method, the test results are not what we want:
[Object Object]
When you create an instance of a class, you typically specify an instance name:
var t:load = new Load ();
The procedure for executing the above code:
L Create an object for the load class and allocate storage space for that object.
L invoke the constructor of the load class to initialize the newly created object.
L declares a reference t of an instance of the load class, which is the instance name, to point to the new object.
Instance properties and instance methods can be used with instance names:
Instance name. Attribute
Instance name. Method Name ()

  Third, the parameter pass of the constructor function

When you create an instance of a class with a constructor, you often initialize the properties of some classes, which are usually passed with the parameters of the constructor.

Class ToolTip {
private Var _str:drawstring;
private Var _shape:shape;
Public Function ToolTip (str:drawstring, Shape:shape) {
_str = str;
_shape = shape;
}
To set and get the code for a property
}

If the parameter name is the same as the property name, you can use the This keyword to point to an instance of the class, passing the arguments in by using the point syntax:

Class ToolTip {
private Var str:drawstring;
private Var Shape:shape;
Public Function ToolTip (str:drawstring, Shape:shape) {
This.str = str;
This.shape = shape;
}
}

Use:

var t:tooltip=new ToolTip (str, rect);

In the parameter transfer of the constructor, there is also the problem of address transfer and numerical transmission, which is particularly noteworthy. Examples of different modes of delivery for the following two parameters:
Address delivery:

Class Box {
function Box (A:array) {
A[0] = 5;
Trace (a[0]);
}
}
var a = ["1"];
var t:box = new Box (a);
Trace (a[0]);
5
5

Numeric Delivery:

Class Box {
function Box (a) {
A = 5;
Trace (a);
}
}
var a = 1
var t:box = new Box (a);
Trace (a);
5
1

A reference to the principle of parameter transfer
In AS2, the overload of a constructor is not supported, that is, multiple constructors are defined in a class, but can be emulated through conditional statements (in fact there is only one constructor), and different code is executed by checking the parameters such as the type of the parameter. In Flash, the constructor of the array class can accept different parameters and produce different instances, and I would like to implement a method similar to this.

  Four, empty constructors and private constructors

If you have an empty constructor in a class (the default is public, see the 1th example above), it means that the class does not need to be initialized because the initialization code for a class is generally placed in the constructor. However, an empty constructor may also be an unfinished constructor that, after writing other classes of code, will supplement the code in the constructor, which is also a common method of programming.
Within a class, if no constructor is defined, AS2 creates an empty constructor, so an empty constructor does not make much sense in as2, but if an empty constructor is declared private, it is of great significance.
1, the private constructor in the abstract class
First, a private constructor can guarantee that it cannot be instantiated externally via the new keyword, because it makes no sense to instantiate an abstract class. Within an abstract class, the common method required for subclasses is generally defined, and the abstract class itself is used only as a uniform data type, and its instantiation is deferred to subclasses.
For example, in Flash's drawing, there are three ways to fill a color: solid fill, linear gradient fill, and radial gradient fill. If we're going to write a fill class, the best way to do this is to create an abstract class and have three concrete fill classes that inherit this abstract class, so that three concrete classes have the same data type and uniform interface methods.
The following is a code example:

Abstract class
Class Brush {
Private Function Brush () {
}
Public function Fill (target:movieclip) {
}
}
Solid Fill class
Class SolidBrush extends Brush {
function SolidBrush (color, alpha) {
}
Public function Fill (target:movieclip) {
}
}
Linear gradient Fill class
Class Linerbrush extends Brush {
function Linerbrush (parameters) {
}
Public function Fill (target:movieclip) {
}
}
Use to define the type of the instance name as Bursh, but the concrete instance can be any of the three inside:
var b:brush=new solidbrush ();
B.fill ();
//
var b:brush=new linerbrush ();
B.fill ();

2. Private constructors in specific classes
The most common scenario in this case is that in singleton mode, a class can only create a unique instance of itself. As the following is a code example:

Class Load {
private static Var _instance:load;
Private Function Load () {
}
public static function getinstance (): Load {
if (load._instance = = undefined) {
return load._instance=new Load ();
}
}
}

Use:

var t:load=load.getinstance ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.