June 6, 2002 - Deriving a Specialized Class from a Generic Class
June 6, 2002 Deriving a Specialized Class from a Generic Class Tips: June 2002
Yehuda Shiran, Ph.D.
|
Employee
class:
class Employee{
var name : String;
var address : String;
function Employee(empName : String){
this.name = empName;
}
function printMailingLabel() {
print(name);
print(address);
}
}
Let's now extend the Employee
class to the Manager
class:
class Manager extends Employee{
var numberOfSubordinates : int;
var compensation : double;
function Manager(mgrName : String, salary: double) {
this.name = mgrName;
this.compensation = salary;
}
private var compensation : double;
function get compensation() : double {
return compensation;
}
}
We construct an instance of the Manager
class as follows:
var Sharon : Manager = new Manager("Sharon Doe", 120000);
Sharon.numberOfSubordinates = 5;
Sharon.address = "250 Stanford Ave., Palo Alto, CA 94305";
We can print the salary and mailing label now:
print(Sharon.compensation);
Sharon.printMailingLabel();
The output should be as follows:
120000
Sharon Doe
250 Stanford Ave., Palo Alto, CA 94305
To learn more about JScript .NET, go to Column 110, JScript .NET, Part IV: Inheritance.