Flash / Flex / ActionScript/Class/Member method

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

Access Control Modifiers for Instance Methods

 

class MyClass {
    internal var varName;
    private var varInt = 1000;
    public function MyClass (name) {
      this.varName = name;
    }
    public function update (  ) {
      this.varInt += 100;
    }
  }



Add method to class

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var cla:MyClass = new MyClass("S");
      
      cla.aMethod(  );
    }
  }
}
internal class MyClass {
    internal var varName;
    private var currentVar = 1000;
    public function MyClass (name) {
      this.varName = name;
    }
    function aMethod (  ) {
      trace(" in aMethod");
    }
  }



Method Parameters and Arguments

 
class MyClass {
    internal var varName;
    private var intValue = 1000;
    public function MyClass (name) {
      this.varName = name;
    }
    public function setInt(numberOf) {
      this.intValue += numberOf;
    }
  }



Method Returning Values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      this.pet = new MyClass();
      var age = this.pet.getDate(  );
    }
  }
}
  internal class MyClass {
    public function MyClass () {
    }
    public function getDate (  ) {
      var currentTime = new Date(  );
      return currentTime;
    }
  }



Update data in a member method

 

  class MyClass {
    internal var varName;
    private var varInt = 1000;
    public function MyClass (name) {
      this.varName = name;
    }
    function update (  ) {
      this.varInt += 100;
    }
  }



Using Methods to Examine and Modify an Object"s State

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var pet:MyClass = new MyClass("Stan");
      var oldName = pet.getName(  );
      pet.setName("Marcos");
    }
  }
}
  class MyClass {
    private var petName;
    private var currentCalories = 1000;
    public function MyClass (name) {
      petName = name;
    }
    public function setName (newName) {
      if (newName.length > 20) {
        newName = newName.substr(0, 20);
      } else if (newName == "") {
        return;
      }
      petName = newName;
    }
    public function getName (  ) {
      return petName;
    }
  }



Using the returning value from method to do the calculation

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      var pet1 = new MyClass();
      var pet2 = new MyClass();
      var totalAge = pet1.getDate() + pet2.getDate(  );
    }
  }
}
  internal class MyClass {
    public function MyClass () {
    }
    public function getDate (  ) {
      var currentTime = new Date(  );
      return currentTime;
    }
  }