In my ActionScript3 class, can I have a property with getter and setter?

In my ActionScript3 class, can I have a property with getter and setter?

+4
source share
3 answers

Well, you can just use the basic getter / setter syntax for any property of your AS3 class. for instance

package { public class PropEG { private var _prop:String; public function get prop():String { return _prop; } public function set prop(value:String):void { _prop = value; } } } 
+20
source

Yes, you can create getter and setter functions inside the AS3 class.

Example:

 private var _foo:String = ""; public function get foo():String{ return _foo; } public function set foo(value:String):void { _foo= value; } 

more information about getters and setters can be found here

+3
source

A getter is a function with a return value depending on what we return. The installer always has one parameter, since we give the variable a new value through the parameter.

First we create an instance of the class containing getter and setter, in our case it is "a". Then we call the setter, if we want to change the variable and using the point syntax, we call the setter function, and using the = operator, we fill in the parameter. To get the value for a variable, we use a getter in the same way as shown in the example (a.myVar). Unlike a regular function call, we omit the parentheses. Do not forget to add the return type, otherwise there will be an error.

package {

 import flash.display.Sprite; import flash.text.TextField; public class App extends Sprite { private var tsecField:TextField; private var tField:TextField; public function App() { myTest(); } private function myTest():void { var a:Testvar = new Testvar(); tField = new TextField(); tField.autoSize = "left"; tField.background = true; tField.border = true; a.mynewVar = "This is the new var."; tField.text = "Test is: "+a.myVar; addChild(tField); } } 

}

import flash.display.Sprite;

import flash.text.TextField;

the Testvar class extends Sprite {public var test: String;

 public function Testvar() { } public function set mynewVar(newTest:String):void { test = newTest; } public function get myVar():String { return test; } 

}

0
source

Source: https://habr.com/ru/post/1277300/


All Articles