Understanding of class, private property and inheritance _flash as

Source: Internet
Author: User
Tags inheritance
1, first, define a basic class, define the method of the class with the "Class" command.
When you create a basic class, note that the definition of the class must be in an external class script file (new-as file), with the file extension "as", and an emphasis on "three unification": the class name, the class script filename, and the constructor name in the class.
Methods: File-New-actionscript file, save, filename named "Chinese. as". Then add the following code inside:

Copy Code code as follows:

The name here should be the same as the file name. I've talked about it before.
Class Chinese {
A skin color property is set, its value is a string type, and the default value is "Yellow"
public var skin color: String = "yellow";
Set a hair color property whose value is a string type and the default value is black
public var hair color: String = "BLACK";
Set up a method function called "Marry wife", return type is string type.
Public Function Marry wife (): String {
Return ("Lift the sedan chair, drink beer, invasion");
}
}

2, use a class, you must first import this class. General Use Import * * * * *;
Method: Create a new Flash file and save it in the same directory you just created. Then add the following code inside:
Copy Code code as follows:

This sentence is a personal habit, you can not add, after the same ...
/*http://www.jb51.net Cloud-dwelling Community * *

/*
Note: Since Flash MX, flash officially supports Unicode encoding, so we can directly use the Chinese name as the variable name.
*/
The introduction of the "Chinese" class, note: The introduction name must be the same as the class name and the class file name, also note that when the class is imported, there is no () behind the class.
Import Chinese;
To establish a "Chinese" class example: Liu MoU
var Hbro: Chinese = new Chinese ();
Output object instance "Liu" 's "Hair" property
Trace ("Hbro Hair color is:" +hbro. Hair color);
Output object instance "Liu" 's "Skin Color" property
Trace ("Hbro skin color is:" +hbro. skin color);
Output object instance "Liu MoU" the value returned by the "Year of the Year" note that after trace, if followed by the method, it cannot be added after the method (). Resolution
Trace ("Hbro Marry Wife:" +hbro.)
/*http://www.jb51.net Cloud-dwelling Community * *
In this way, the creation and application of a simple class is successful.
3, the use of private properties. Here, we are in sync with inheritance. Now we have a new "Chongqing people. As" class, and then add the following code:
Copy Code code as follows:

Class Chongqing people extends Chinese {
public var personality: String = "like to eat chili";
Private var feature: String;
Public function outer (): String {
this. Features = "Handsome, suave, fascinated by thousands of girls";
Return (this. feature);
}
}
Note that the third line is private. Generally speaking, it is necessary to use a common function to connect private properties when using private properties. Note that inheritance uses Exstends.
Class A extends B means, a inherits from B, as if a large business owner had died, his inheritance had to be inherited by his son. The meaning here is that a inherits the properties of B. For example, the boss surnamed Huang, his son must follow the surname yellow a reason.
Next, let's look at the application of inheritance first. Replace the following code in the file created in step two:
Copy Code code as follows:

/*http://www.jb51.net Cloud-dwelling Community * *
/*
Note: Since Flash MX, flash officially supports Unicode encoding, so we can directly use the Chinese name as the variable name.
*/
The introduction of the "Chinese" class, note: The introduction name must be the same as the class name and the class file name, also note that when the class is imported, there is no () behind the class.
Import Chinese;
To establish a "Chinese" class example: Liu MoU
var Hbro: Chinese = new Chinese ();
Output object instance "Liu" 's "Hair" property
Trace ("Hbro Hair color is:" +hbro. Hair color);
Output object instance "Liu" 's "Skin Color" property
Trace ("Hbro skin color is:" +hbro. skin color);
Output object instance "Liu MoU" the value returned by the "Year of the Year" note that after trace, if followed by the method, it cannot be added after the method (). Resolution
Trace ("Hbro Marry Wife:" +hbro.)
Define a Chongqing man named Sand.
var sand: chongqing people = new Chongqing people ();
The character of the output sand
Trace ("Character of the Sand": + sand, personality);
In order to test whether the people of Chongqing have succeeded in inheriting from the parents "Chinese", test the color of the sand
Trace ("Skin color of the sand:" + sand. skin color);
Trace ("The appearance of the sand:" + sand, appearance ());
/*http://www.jb51.net Cloud-dwelling Community * *

Pay attention to the difference between ...
The newly added sections are as follows:
Copy Code code as follows:


Define a Chongqing man named Sand.
var sand: chongqing people = new Chongqing people ();
The character of the output sand
Trace ("Character of the Sand": + sand, personality);
In order to test whether the people of Chongqing have succeeded in inheriting from the parents "Chinese", test the color of the sand
Trace ("Skin color of the sand:" + sand. skin color);
Trace ("The appearance of the sand:" + sand, appearance ());
Press the Ctrl+enter key to see the effect.

Download the above source address:
Http://img.jb51.net/downtools/classandexstends.rar

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.