Using getters and setters in Actionscript 3

This post briefly explains on how to use getters and setters in Actionscript3.
Let us consider a class named “Test”

public class Test
private var wordName:String ="Porsche";
public function Test()
//getter function which return string wordName
public function get stringName():String
return wordName;
//setter function which sets the string value
public function set stringName(str:String):void
wordName = str;

Now let us use this getter and setter say in actions frame in .fla file or in any external .as file in following manner

//creating an object of class Test
var test:Test = new Test();
trace(test.stringName) //Porsche is displayed
test.stringName = "Range Rover";
//the variable wordName of class Test is changed to Range Rover and word Range Rover is displayed


2 thoughts on “Using getters and setters in Actionscript 3

  1. Thanks for the illustration.

    Maybe I’m missing something.

    If I have two classes, maybe more, that all need to access the same getter/setter, how would I do that?

    Much thanks,

    Don B. in Toronto.

    • Hi Don B,
      Could you please elaborate on your problem.
      Basically getters and setters are used to modify some properties of a class by its object.
      I think you are opting for static methods of a class.
      If you can illustrate more I am happy to help

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s