Flash / Flex / ActionScript/Class/Access Control Modifiers

Материал из Web эксперт
Перейти к: навигация, поиск

Access Control Modifiers for Classes are listed before the keyword class in a class definition

   <source lang="java">

attribute class ClassIdentifier { } To add the public attribute to the MyClass class package com.wbex {

 public class MyClass {
 }

} Explicitly indicate that MyClass is used within the com.wbex package only with internal attribute package com.wbex {

internal class MyClass {
}

} A class defined with the internal attribute can be used within its containing package only.

       </source>
   
  


Declares a new private property called _id within the Example class:

   <source lang="java">

package {

   public class Example {
       private var _id:String;
   }

}

       </source>
   
  


Inherit protected variable

   <source lang="java">

   class Toaster
   {
       protected var finish:String = "Reflective Silver";
   }
   class HeatedResistorToaster extends Toaster
   {
       public function HeatedResistorToaster()
       {
           trace(finish); //Reflective Silver
       }
   }
       </source>
   
  


Internal variable

   <source lang="java">

   class Bread
   {
       internal var toastiness:Number = 0;
   }
   class Pillow
   {
       public function Pillow()
       {
           var b:Bread = new Bread();
           b.toastiness = 20; //Compiler error! You can"t do this!
       }
   }
       </source>
   
  


private variable is not inherited

   <source lang="java">

   class Toaster
   {
       private var finish:String = "Reflective Silver";
   }
   class HeatedResistorToaster extends Toaster
   {
       public function HeatedResistorToaster()
       {
           trace(finish); //Compiler error! We don"t have a finish.
       }
   }
       </source>
   
  


protected member variables

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var r = new Rectangle(  );
       r.setSize(4,5);
       trace(r.getArea(  )); 
       
       var s = new Square(  );
       s.setSize(4,5);
       trace (s.getArea());  
   }
 }

} class Rectangle {

 protected var w = 0;
 protected var h = 0;
 public function setSize (newW, newH) {
   w = newW;
   h = newH;
 }
 public function getArea (  ) {
   return w * h;
 }

} class Square extends Rectangle {

 override public function setSize (newW, newH) {
   if (newW == newH) {
     w = newW;
     h = newH;
   }
 }

}

       </source>
   
  


The Internal Details

   <source lang="java">

package {

 public class SomeClass {
   public function instanceMeth (  ):void {
     function nestedFunc (  ):void {
       trace(a);
     }
   }
 }

} var a:int = 15;

       </source>