Defining properties
Standard Gosu style is to use public properties backed by private variables instead of using public variables.
public var FirstName : String // IncorrectFirstName property and stores its value in the _firstname
variable:private var _firstName : String as FirstNameGosu variable alias syntax provides this shortcut to declare an instance variable as a property.
Variable alias syntax uses the as keyword followed by the property name to make
simple getter and setter property methods backed by an class instance variable. Use this approach to specify a
direct mapping between a publicly visible property and the private variable that contains the property's value. A
property defined in this way is both readable and writable.
_firstName, code that accesses this data does
not access the private instance variable directly. Any code that needs to use the data uses the period symbol
(.) and the property
name:var e = new Employee()
e.FirstName = "Pam" // Sets to "Pam" by calling the property setter
var g = e.FirstName // Calls the property getterCode within a class does not need to use the property name to access a property on the same class. Classes can
access their own private variables. In the previous example, other methods in that class could reference the
_firstName variable rather than using the property accessor FirstName.
Writing property getter and setter methods explicitly
If you need to perform additional processing before storing or retrieving property values, you can write either
or both of the getter and setter methods for the property. This form of a property definition is like a method
that uses the keywords property get or property set before the method name
instead of function. The property getter method must take zero parameters and the property
setter method must take exactly one parameter.
Your property getter and setter methods can perform complex calculations or store the data in some other way than as a variable.
FullName, FirstName, and
LastName properties are consistent in the setter methods for the FirstName,
and LastName properties. The code also checks whether a user is authorized to access a social
security number in the getter and setter methods for the SSN
property.package doc.example
uses gw.api.util.StringUtil
class Employee {
var _fullName : String as readonly FullName
var _firstName : String as FirstName
var _lastName : String as LastName
var _ssn : String as SSN
construct(name : String) {
setAllNames(name)
}
property get SSN() : String {
return (isUserAuthorized() ? _ssn : "Not authorized to see SSN")
}
property set SSN(ssn : String) {
if (isUserAuthorized()) {
_ssn = ssn
} else {
print("Not authorized to set SSN")
}
}
property set FirstName(fName : String) {
setAllNames(fName + " " + _lastName)
}
property set LastName(lName : String) {
setAllNames(_firstName + " " + lName)
}
private function setAllNames(name : String) {
var allNames = StringUtil.splitWhitespace(name)
_firstName = allNames[ 0]
_lastName = allNames[allNames.Count - 1]
_fullName = _firstName + " " + _lastName
}
private function isUserAuthorized() : boolean {
// Dummy method
return false
}
}var emp = new Employee("Pam Trujillo")
emp.SSN = "123456789"
print("First name:\t${emp.FirstName}")
print("Last name:\t${emp.LastName}")
print("Full name:\t${emp.FullName}")
print("SSN:\t${emp.SSN}")Not authorized to set SSN First name: Pam Last name: Trujillo Full name: Pam Trujillo SSN: Not authorized to see SSN
Read-only and write-only properties
readonly before the property name, as shown in the following code:
var _fullName : String as readonly FullNameGosu does not provide a keyword to make a write-only property in this shortcut syntax.
var _secretID : int
property set SecretID(max : int) {
_secretID = (Math.random() * max) as int
}