|
Listing 12-12 extracted from chapter
Inheritance, polymorphism and abstraction
Listing 12-11< > Listing 12-13
This listing can be compiled with the command line: csc.exe /target:exe Example_12_12.cs Errors: 0 Warnings: 0
Example_12_12.cs
interface I {
void f(int i);
void g(int i);
}
class FooBase : I {
public virtual void f(int i) {
System.Console.WriteLine("FooBase.f({0})", i);
}
public void g(int i) {
System.Console.WriteLine("FooBase.g({0})", i);
}
}
class FooDerived : FooBase {
public override void f(int i) {
System.Console.WriteLine("FooDerived.f({0})", i);
}
}
class Program {
static void Main() {
FooBase refB1 = new FooBase();
I refI1 = refB1;
FooDerived refD= new FooDerived();
FooBase refB2 = refD;
I refI2 = refD;
refB1.f(1);
refI1.f(2);
refD.f(3);
refB2.f(4);
refI2.f(5);
}
}
Copyright Patrick Smacchia 2006 2007
|