|
Listing 12-2 extracted from chapter
Inheritance, polymorphism and abstraction
Listing 12-1< > Listing 12-3
This listing can be compiled with the command line: csc.exe /target:exe Example_12_2.cs Errors: 0 Warnings: 0
Example_12_2.cs
public class Employee {
// The field m_Name is visible from derived classes.
protected string m_Name;
public Employee( string name ) { m_Name = name; }
public virtual void DisplayDescription() {
System.Console.Write( "Name: {0}", m_Name );
}
}
class Secretary : Employee { // Secretary inherits Employee.
public Secretary( string name ) : base( name ) {}
public override void DisplayDescription() {
// Call the Employee version of the DisplayDescription() method.
base.DisplayDescription();
System.Console.Write( " Position: Secretary\n" );
}
}
class Technician : Employee { // Technician inherits Employee.
public Technician( string name ) : base( name ) {}
public override void DisplayDescription(){
// Call the Employee version of the DisplayDescription() method.
base.DisplayDescription();
System.Console.Write( " Position: Technician\n");
}
}
class Program {
static void Main() {
Employee[] array = new Employee[3];
array[0] = new Technician("Line");
array[1] = new Secretary("Lisanette");
array[2] = new Secretary("Anne-Mette");
foreach( Employee employee in array )
employee.DisplayDescription();
}
}
Copyright Patrick Smacchia 2006 2007
|