Dalam sebuah Class dalam ActionScript Anda juga dapat membuat elemen statis (variable, metode, constante). Elemen statis secara eksklusif dimiliki oleh Class, dan tidak dapat diakses melalui sebuah instance dari Class, tetapi langsung menggunakan nama Class. • Untuk membuat elemen statis, Anda menggunakan kata kunci static dalam definisi elemen.
Sebagai contoh :Static properties
package {
// "cthstatic" class
public class cthstatic
{
// properties
public var prop1:String = 'http://blog.pustakaflash.com';
public static var prop2:String = 'ActionScript 3'; // Static variable
}
}
nah sekarang buat fla document
// Create an instance of the "cthstatic" class
var obj:cthstatic = new cthstatic();
// access the class property
trace(obj.prop1); // ini akan menghasilkan http://blog.pustakaflash.com
// trace(obj.prop2); // ini aka error
// acces 'prop2' directly through the class name
trace(cthstatic.prop2); // ini akan menghasilkan ActionScript 3
Static constants
package {
// "cthstatic" class
public class cthstatic
{
// properties
public var prop1:String = 'http://blog.pustakaflash.com';
public static var prop2:String = 'ActionScript 3'; // Static variable
// Defining constants
public const TUTORIALS:String = 'Flash';
public static const SITE:String = 'Pustaka Flash'; // Static constants
}
}
dalam fla document
// Create instance of the class "cthstatic"
var obj2:cthstatic = new cthstatic();
// access the class constants
trace(obj2.TUTORIALS); // ini akan menghasilkan Flash
// trace(obj2.SITE); // Returns an error
// access 'SITE' using the class name
trace(cthstatic.SITE); //Pustaka Flash
Static Methods
package {
// "cthstatic" class
public class cthstatic
{
// properties
public var prop1:String = 'http://blog.pustakaflash.com';
public static var prop2:String = 'ActionScript 3'; // Static variable
// Defining constants
public const TUTORIALS:String = 'Flash';
public static const SITE:String = 'Pustaka Flash'; // Static constants // Static constant
// Defining a static method
public static function Courses(param:String):String
{
var re:String; // The variable that will be returned
// define "switch" instruction to set different posible values for "re"
switch(param)
{
case 'Flash':
re = cthstatic.prop2; // "re" receives the value of the static property "prop2"
break;
case 'Pustaka Flash':
re = SITE; // "re" receives the value of the static constant "SITE" ( or cthstatic.SITE; )
break;
default: re = 'Default value';
}
return re; // Returns "re" value
}
}
}
dalam fla document
var obj3:elStatic = new elStatic();
trace(elStatic.Courses(obj3.TUTORIALS)); // Flash
// trace(obj3.Courses(cthstatic.SITE)); // generate an error
// Call the static method, uses a static constant for argument
trace(cthstatic.Courses(cthstatic.SITE)); // Pustaka Flash
By Ricko nada di Pustaka Flash terima kasih telah membaca :) Class Static Elements For ActionScript 3
0 komentar:
Post a Comment