Prototype objects in actionscript3

Source: Internet
Author: User

AS3mso-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> and javamso-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> different languages, in which everything is an object, even a class is an object, all classes, including any classes we define. Their prototype objects are objectmso-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> class.

MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> first, we use a simple example to describe prototypemso-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> have a perceptual and intuitive understanding. Prototypemso-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> is a static attribute in a class, which indicates reference to the class's prototype object.

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> let's first define two classes that satisfy the inheritance relationship:

Public dynamic class parentclass {}
Public dynamic class childclass extends parentclass {
}

Then we write in the script:

Trace (parentclass. prototype, childclass. Prototype );

MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> result output:

[Object object] [object]

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> the result [object]
Minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin ">, we can see that the first object calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> indicates an object, the second objectmso-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> stands for objectmso-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> class, which means that this is an object, he is objectmso-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> class.

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> WE can dynamically add attributes to the prototype object and add them to the prototypecalibri of the parent class; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Attributes added in minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> can be shared by the quilt class, the following example shows how to use prototypecalibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> adds a sayhellocalibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> function. Then we can access it in the subclass. In contrast, if we are in prototypecalibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin ">, other sub-classes cannot be accessed. That is, you can only use this class to call the attributes added to the prototype.

Public class parentclass
{
Prototype. sayhello = function (): String
{
Return "Hello, ptototype ";
}
}
Public class childone extends parentclass
{
}
Public class childtwo extends parentclass
{
}

Public Function oncreate (): void
{
VaR strinfo: String = childone. Prototype. sayhello ();
Trace (strinfo );
VaR str1: String = childtwo. Prototype. sayhello ();
Trace (str1 );
Childone. Prototype. testfunc = function (): void
{
Trace ("just for test .");
}
Childone. Prototype. Test ();
// Childtwo. Prototype. Test ();
}

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> the class instance differs from the class in terms of prototype objects. The initial prototype objects of a class instance are undefined, neither o bjectcalibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> classes are not the classes that define this instance either. That is to say, when we instantiate a class, the prototype object of this instance is undefined.

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> we have defined childonemso-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> class for instantiation:

VaR C1: childone = new childone ();
Trace (c1.prototype );

Test output:

Undefined

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> however, We can dynamically assign values to the prototype attribute of an instance, provided that the class must be a dynamic class. The Code is as follows:

VaR C1: childone = new childone ();

C1.prototype = childtwo;
Trace (c1.prototype );

Test output:

[Class childtwo]

MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> This indicates that its prototype object is a class,
Minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin "> Class Name: childtwocalibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:; MSO-Fareast-theme-Font:
Minor-Fareast; MSO-Hansi-font-family: calibri; MSO-Hansi-theme-Font: minor-Latin ">.

MSO-ascii-font-family: calibri; MSO-ascii-theme-Font: minor-Latin; MSO-Fareast-font-family:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family: calibri;
MSO-Hansi-theme-Font: minor-Latin "> at last, I want to add some knowledge about namespaces. MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-font-family:
Calibri; MSO-Hansi-theme-Font: minor-Latin "> Functions, variables, and constants can be placed in a namespace, but class definitions cannot be placed. The following is an example of a simple namespace:

Public namespace MySpace = "http://phinecos.cnblogs.com ";


MySpace function myfunc (): void
{
Trace ("test namespace .");
}

Use namespace MySpace;
Myfunc ();
MySpace: myfunc ();

 

 

 

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.