tailieunhanh - Essential ActionScript 3.0 PHẦN 5

Example 17-5. The ShelteredClass class (continued) return null; } } } Now let’s look at Caller, a class that wishes to access ShelteredClass’s restricted methods and variables. Having already seen the authorizedClasses array in ShelteredClass, we know that Caller is a legal class. In our example, Caller is also the main application class, so it extends Sprite. The Caller class creates an instance of ShelteredClass in its constructor method and assigns that instance to the variable shelteredObject. package { import .*; public class Caller extends Sprite { private var shelteredObject:ShelteredClass; public function Caller ( ) { shelteredObject = new ShelteredClass( ); } }. | Example 17-5. The ShelteredClass class continued return null _ _ Now let s look at Caller a class that wishes to access ShelteredClass s restricted methods and variables. Having already seen the authorizedClasses array in ShelteredClass we know that Caller is a legal class. In our example Caller is also the main application class so it extends Sprite. The Caller class creates an instance of ShelteredClass in its constructor method and assigns that instance to the variable shelteredObject. package import . public class Caller extends Sprite private var shelteredObject ShelteredClass public function Caller shelteredObject new ShelteredClass _ _ To invoke secretMethod on ShelteredClass a Caller object must first retrieve a reference to the restricted namespace. To do so the Caller object passes itself to getRestrictedNamespace and assigns the result either restricted or null to a variable key for later use. var key Namespace this Then before calling secretMethod Caller first checks whether key refers to a valid namespace. If it does then Caller uses key as the namespace when invoking secureMethod if key null secureMethod For convenience our Caller class wraps the code that calls secretMethod in a method named callSecretMethod public function callSecretMethod void var key Namespace this if key null secretMethod _ Example 17-6 shows the entire code for the Caller class including callSecretMethod and another convenience method displaySecret which accesses the restricted variable secretData using the same basic technique. Applied Namespace Examples 345 Example 17-6. The Caller class package import . public class Caller extends Sprite private var shelteredObject ShelteredClass public function Caller shelteredObject new ShelteredClass callSecretMethod displaySecret public function callSecretMethod void var key Namespace .

TỪ KHÓA LIÊN QUAN