Flex set and get keyword usage

Flex set and get keyword usage

1, set

set definition keyword usage function set property (newValue: *): void {// your statements here}

language version: ActionScript 3.0 RuntimeVersions: Flash Player 9

Define a setter method to be used as an attribute in the public interface. A setter is a special method that can set a value for an attribute declared with the var keyword. Unlike other methods, there is no need to use parentheses (()) when calling the setter, which makes the setter look like a variable.

You can use setters to create public interfaces for private attributes, and then apply the principle of information hiding. The advantage of hiding information is that even if the basic implementation of the private attribute changes, the public interface remains the same.

Another advantage of setters is that they can be overridden in subclasses, while properties declared with var cannot.

The return type of the setter must be void or not specify any type.

Setters can be combined with getters to create readable and writable properties. To create a read-only property, you need to create a getter without a corresponding setter. To create a write-only property, you need to create a setter without a corresponding getter.

Note: This keyword is only supported in external script files, but not in scripts written in the “Actions” panel.

Parameters property:* — The identifier of the property modified by set; this value must be the same as The corresponding get command uses the same value. newValue:* — the new value to be assigned.
Example
How to use example
The following example creates a readable and writable attribute age by defining a getter-setter. package {class User {Enter in your script private var userAge:Number; in your script public function get age():Number return userAge; void set: x: Number public function age (x: Number) Code: var myUser:User = new User();myUser.age = 25;trace(myUser.age); // 25 2, getgetdefinition keyword Usage function get property(): returnType{ / / your statements here}

Language version: ActionScript 3.0 ActionScript 3.0 ActionScript 3.0 , The method can be read like an attribute. A getter is a special function that returns the value of an attribute declared using the var or const keyword. Unlike other methods, the parentheses (()) are not required when calling the getter, which makes the getter look like a variable.

You can use getters to create public interfaces for private attributes, and then apply the principle of information hiding. The advantage of hiding information is that even if the basic implementation of the private attribute changes, the public interface remains the same.

Another advantage of getters is that they can be overridden in subclasses, while properties declared with var or const cannot.

The getter can be used in conjunction with the setter to create readable and writable properties. To create a read-only property, you need to create a getter without a corresponding setter. To create a write-only property, you need to create a setter without a corresponding getter.

Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or more on the “Flash” tab of the “Publish Settings” dialog box of the FLA file High version. This keyword is only supported in external script files, but not in scripts written in the “Actions” panel.

Parameter property:*get The identifier of the property accessed; this value must be the same as The values ​​used in the corresponding set commands are the same. returnType:* — The data type of the return value.
Examples
How to use examples
The following example defines a Team class. The Team class contains getter and setter methods that allow you to retrieve and set the attributes of the class: package {public class Team {var teamName:String; var teamCode:String; var teamPlayers:Array = new Array() ; public function Team (param_name: String, param_code: String) {teamName = param_name; teamCode = param_code;} public function get name (): String {return teamName;} public function set name (param_name: String): void {teamName = param_name;}}} Enter the following code in your script: var giants:Team = new Team(“San Fran”, “SFO”); trace(giants.name); giants.name = “San Francisco”; trace( giants.name); /*San Fran San Francisco */

When tracking giants.name, use the getter method to return the value of this property.

1, set

set definition keyword Usage function set property (newValue: * ): void{ // your statements here}}}}}}}}}}}}}}}} â?? •

Language version: ActionScript 3.0 Run s p p >Define a setter method to be used as a property in the public interface. A setter is a special method that can set a value for an attribute declared with the var keyword. Unlike other methods, there is no need to use parentheses (()) when calling the setter, which makes the setter look like a variable.

You can use setters to create public interfaces for private attributes, and then apply the principle of information hiding. The advantage of hiding information is that even if the basic implementation of the private attribute changes, the public interface remains the same.

Another advantage of setters is that they can be overridden in subclasses, while properties declared with var cannot.

The return type of the setter must be void or not specify any type.

Setters can be combined with getters to create readable and writable properties. To create a read-only property, you need to create a getter without a corresponding setter. To create a write-only property, you need to create a setter without a corresponding getter.

Note: This keyword is only supported in external script files, but not in scripts written in the “Actions” panel.

Parameters property:* — The identifier of the property modified by set; this value must be the same as The corresponding get command uses the same value. newValue:* — the new value to be assigned.
Example
How to use example
The following example creates a readable and writable attribute age by defining a getter-setter. package {class User {Enter in your script private var userAge:Number; in your script public function get age():Number return userAge; void set: x: Number public function age (x: Number) Code: var myUser:User = new User();myUser.age = 25;trace(myUser.age); // 25 2, getgetdefinition keyword Usage function get property(): returnType{ / / your statements here}

Language version: ActionScript 3.0 ActionScript 3.0 ActionScript 3.0 , The method can be read like an attribute. A getter is a special function that returns the value of an attribute declared using the var or const keyword. Unlike other methods, the parentheses (()) are not required when calling the getter, which makes the getter look like a variable.

You can use getters to create public interfaces for private attributes, and then apply the principle of information hiding. The advantage of hiding information is that even if the basic implementation of the private attribute changes, the public interface remains the same.

Another advantage of getters is that they can be overridden in subclasses, while properties declared with var or const cannot.

The getter can be used in conjunction with the setter to create readable and writable properties. To create a read-only property, you need to create a getter without a corresponding setter. To create a write-only property, you need to create a setter without a corresponding getter.

Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or more on the “Flash” tab of the “Publish Settings” dialog box of the FLA file High version. This keyword is only supported in external script files, but not in scripts written in the “Actions” panel.

Parameter property:*get The identifier of the property accessed; this value must be the same as The values ​​used in the corresponding set commands are the same. returnType:* — The data type of the return value.
Examples
How to use examples
The following example defines a Team class. The Team class contains getter and setter methods that allow you to retrieve and set the properties of the class: package {public class Team {var teamName:String; var teamCode:String; var teamPlayers:Array = new Array() ; public function Team (param_name: String, param_code: String) {teamName = param_name; teamCode = param_code;} public function get name (): String {return teamName;} public function set name (param_name: String): void {teamName = param_name;}}} Enter the following code in your script: var giants:Team = new Team(“San Fran”, “SFO”); trace(giants.name); giants.name = “San Francisco”; trace( giants.name); /*San Fran San Francisco */

When tracking giants.name, use the getter method to return the value of this property.

Leave a Comment

Your email address will not be published.