PART I
C h a p t e r 1 1 :
I n h e r i t a n c e
285
PART IPART I
public int i = 0;
// Show() in A
public void Show() {
Console.WriteLine("i in base class: " + i);
}
}
// Create a derived class.
class B : A {
new int i; // this i hides the i in A
public B(int a, int b) {
base.i = a; // this uncovers the i in A
i = b; // i in B
}
// This hides Show() in A. Notice the use of new.
new public void Show() {
base.Show(); // this calls Show() in A
// this displays the i in B
Console.WriteLine("i in derived class: " + i);
}
}
class UncoverName {
static void Main() {
B ob = new B(1, 2);
ob.Show();
}
}
The output from the program is shown here:
i in base class: 1
i in derived class: 2
As you can see,
base.Show( )
calls the base class version of
Do'stlaringiz bilan baham: |