Try the first custom class:
// Create an ActionScript 3.0 project and save the project. // then, create an ActionScript 3.0 class. Enter the class name ctest according to the Wizard. The automatically generated code is as follows: package {public class ctest {public function ctest () {// The method with the same name as the class is the constructor trace ("Hello ctest! "); // This line is manually added. Save the modified file before testing. as) is saved in the same directory as the current project, and then written into the first frame of the project timeline action: var OBJ: ctest = new ctest (); // then the test runs, the test output is Hello ctest!
About package name and Class Name:
// The ctest class created above belongs to a package. The package name is used to indicate the file path. The above anonymous package indicates the file and *. FLA is in the same directory // if you want to put ctest. as is saved in the project directory AAA \ BBB \, the package name should be AAA. BBB; inferred: If a directory contains multiple *. as file, where the package name must be consistent/* AAA \ BBB \ ctest. as */package AAA. bbb {public class ctest {public function Method1 () {trace ("Method1");} public function method2 () {trace ("method2 ");}}} /* code in the Key Frame in FLA */import AAA. bbb. ctest; // This line is automatically added when the code is entered. The import AAA can be used. bbb. *; import all the packages in the directory var OBJ: ctest = new ctest (); // You can also write var OBJ: AAA. bbb. ctest = new AAA. bbb. ctest (); but if there is no name conflict, there is no need for obj. method1 (); obj. method2 (); // each package can have only one class. The class name must be the same as the file name. The file can have other helper classes, but must be placed outside the package. // a source file may have no package or class, but only some constants or variables. // packages can also be nested, such as the official flash. the display package is a flash package. However, you do not need to use nested packages to write your own programs.
Visibility of classes and class members:
/* Class visibility: */dynamic // dynamic class. You can add final/sealed classes to the instance at runtime, and cannot be visible in the inherited internal // package (default) public // visible inside and outside the package (commonly used)/* class member visibility: */internal // visible in the package (default) private // visible in the class protected // visible in the class and derived class public // inside and outside the class, inside and outside the package are visible static // static members; instance userdefinednamespace that belongs to this class but does not belong to this class // use a custom namespace to specify the visibility
Static members and instance members:
// The static member of the class is called by class name. The instance member of the class is called by the instantiated object // ------------------------------- package {public class ctest {public Static Var fcount: Int = 0; // static variable public static const Pi = 3.14; // static constant public static function Method1 () {trace ("Method1");} // static method public function method2 () {trace ("method2");} // instance method} // ----------------------------------- ctest. fcount = 123; trace (ctest. fcount); trace (ctest ["fcount"]); trace (ctest. pi); trace (ctest ["Pi"]); ctest. method1 (); ctest ["Method1"] (); var OBJ: ctest = new ctest (); obj. method2 (); OBJ ["method2"] (); // as3 also allows the static method to have the same name as the instance method, but there is no reason to use it like this.
Constructor:
// The class in as3 can only have one constructor. Because its visibility must be public, public can be omitted, and the constructor cannot return values. // if not defined, the system will use the default constructor without parameters // ------------------------------- package {public class ctest {public var fstr: string; Public Function ctest (STR: string) {fstr = STR ;}}// ----------------------------------- var OBJ: ctest = new ctest ("ABC"); trace (obj. fstr );
Usage attributes:
//---------------------------------package { public class CTest { private var fCount:int = 0; public function get Count():int { return fCount; } public function set Count(aCount:int) { fCount = (aCount > 0) ? aCount : 0; } }}//---------------------------------var obj:CTest = new CTest();obj.Count = 99;trace(obj.Count); //99obj.Count = -1;trace(obj.Count); //0
Test the binding method (input or output method:
//---------------------------------package { public class CTest { private function Method1() { trace(this); } public function Method2():Function { return Method1; } public function Method3(aFun:Function) { aFun(); } }}//---------------------------------function myFunc1() { trace(this); }myFunc1(); //[object MainTimeline]var obj:CTest = new CTest();var myFunc2:Function = obj.Method2();myFunc2(); //[object CTest]obj.Method3(myFunc2); //[object CTest]
Use the class to simulate enumeration:
// This type generally does not need to be inherited (final), and the Members should also be static constants (static const) // ------------------------------- package {public final class ctest {public static const male: string = "male"; public static const female: String = "female" ;}}// --------------------------------- var age: string; age = "female "; // age = "male"; Switch (AGE) {Case ctest. male: trace ("male"); break; Case ctest. female: trace ("female"); break; default: trace ("? ");}
The namespace of as3:
// The "package" of as3 is similar to the "namespace" of other languages, but it actually indicates that the "namespace" of the relative path // as3 only controls the visibility of class members, its function is similar to public, private, protected, and internal. // I feel that the design is poor. Try not to use it.
Dynamic (dynamic) class:
// Dynamic class instances can dynamically add members // ----------------------------------------- package {public dynamic class ctest {public const name = "ctest" ;}// ------------------------------- var: ctest = new ctest (); trace (obj. name); // ctestobj. A = 123; trace (obj. a); // 123obj. B = function () {trace ("ctest B") ;}; obj. B (); // ctest bfunction myfunc () {trace ("myfunc");} obj. C = myfunc; obj. C (); // myfunc
Prototype:
//---------------------------------package { public dynamic class CTest { public const Name = "CTest"; }}//---------------------------------CTest.prototype.A = 123;CTest.prototype.B = function () { trace("CTest B"); };//function myFunc() { trace("myFunc"); }//CTest.prototype = myFunc;var obj:CTest = new CTest();trace(obj.Name); //CTesttrace(obj.A); //123obj.B(); //CTest B//obj.C(); //
Class inheritance:
// The base class, which is saved in the cbase directory of the project. aspackage {public class cbase {public function basemethod () {trace ("basemethod") ;}}// subclass, Which is saved in the project directory ctest. aspackage {public class ctest extends cbase {// inheritance keyword extends public function testmethod () {trace ("testmethod") ;}}// test var OBJ: ctest = new ctest (); obj. basemethod (); // basemethodobj. testmethod (); // testmethod
Inherit from interface:
// As3 does not support abstract classes, but supports interfaces // interfaces can only use public and internal access control specifiers. // interfaces cannot contain variables or constants, however, the method format must be consistent when the method that can contain the property // inherited interface, but the parameter name and default value of the parameter can be different // when inheriting multiple interfaces, you can use commas to separate them. // interface, save IBASE in the project directory. aspackage {public interface IBASE {function interfacemethod (); // API members are public if they do not use visibility modification.} // sub-classes are saved in the project directory ctest. aspackage {public class ctest implements IBASE {public function interfacemethod () {trace ("interfacemethod");} public function testmethod () {trace ("testmethod ");}}} // test var OBJ: ctest = new ctest (); obj. interfacemethod (); // interfacemethodobj. testmethod (); // testmethod
Also similar and interface inheritance:
// Interface, which is saved in the project directory IBASE. aspackage {public interface IBASE {function interfacemethod (); // The interface members do not use visibility modification and are all public} // base class, which is saved in the cbase under the project directory. aspackage {public class cbase {public function basemethod () {trace ("basemethod") ;}}// subclass, Which is saved in the project directory ctest. aspackage {public class ctest implements IBASE {public function interfacemethod () {trace ("interfacemethod");} public function testmethod () {trace ("testmethod ");}}} // test var OBJ: ctest = new ctest (); obj. interfacemethod (); // interfacemethodobj. basemethod (); // basemethodobj. testmethod (); // testmethodvar I: IBASE = new ctest (); I. interfacemethod (); // interfacemethodvar base: cbase = new ctest (); base. basemethod (); // basemethod
Override ):
// Cbase. aspackage {public class cbase {Public Function Method () {trace ("basemethod") ;}}// ctest. aspackage {public class ctest extends cbase {override public function method () {trace ("testmethod") ;}}// test var base: cbase = new ctest (); base. method (); // testmethod // only the instance method can be overwritten. The format must be the same and the access level is the same./static methods cannot be overwritten, it will not be overwritten by the inherited // final method.
Use the parent class in the subclass:
// Cbase. aspackage {public class cbase {public function basemethod () {trace ("basemethod") ;}}// ctest. aspackage {public class ctest extends cbase {public function testmethod () {super. basemethod (); // keyword super trace ("testmethod") ;}}// test var OBJ: ctest = new ctest (); obj. testmethod (); // basemethod/testmethod
Inheritance and overwrite of attributes:
// Cbase. aspackage {public class cbase {private var fnum1: int; private var fnum2: int; Public Function get num1 (): int {return fnum1;} public function set num1 (Num: INT) {fnum1 = num;} public function get num2 (): int {return fnum2;} public function set num2 (Num: INT) {fnum2 = num ;}}// ctest. aspackage {public class ctest extends cbase {private var fnum2: int; override public function get num2 ( ): Int {return fnum2;} override public function set num2 (Num: INT) {fnum2 = (Num> 0 )? Num: 0 ;}}// test var OBJ: ctest = new ctest (); obj. num1 = 123; trace (obj. num1); // 123var base: cbase = new ctest (); base. num1 =-1; trace (base. num1); //-1base. num2 =-1; trace (base. num2); // 0