
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Properties
|
77
For example, in the following code, the class property Employee.defaultSalary is
inherited by Manager:
// Code in Employee.as
class Employee {
public static var defaultSalary:Number = 34000;
}
// Code in Manager.as
class Manager extends Employee {
}
If, outside of the Employee and Manager classes, we attempt to access Manager.
defaultSalary
before referencing the Employee class, then the property will be
undefined:
// Fails when this code appears outside of Employee.as and Manager.as
trace(Manager.defaultSalary); // Displays: undefined
To fix the problem, we simply refer to the Employee class before accessing the
property:
// Refer to Employee
Employee;
// Now it works
trace(Manager.defaultSalary); // Displays: 34000
Note, however, that the inherited class property bug does not cause problems for
code inside the subclass. For example, the following code in Manager works without
any special reference to Employee:
class Manager extends Employee {
function paySalary ( ):Void {
// This reference is valid because Employee is referenced
// in the Manager class definition.
var salary:Number = Manager.defaultSalary;
// Remainder of method not shown...
}
}
Compile-Time Constant Expressions
Earlier, we learned that an instance property definition can assign a default value to a
property, provided ...