|
public interface Iaa
{
void show ();
}
class ba // base class
{
public void show ()
{
Console.WriteLine ("ba.show");
}
}
class de: ba, Iaa // Subclass
{
new public void show ()
{
Console.WriteLine ("de.show");
}
}
class Program
{
static void Main (string [] args)
{
b = new de ();
b.show (); // why show ba.show
((Iaa) b) .show (); // why show de.show
Console.Read ();
}
}
class de: ba, Iaa // Subclass
It was originally written that it could not be compiled and passed, it should be that the Show () method of the interface Iaa was not implemented in de
But here happens because de also inherits ba, and there is exactly the function of Show () in ba
The name is the same as the method that needs to implement the interface
(A lot of coincidences -_- "But is there a necessity behind the coincidence)
In fact, this operation is done here so that Show () of the base class ba serves as the implementation of Show () of the interface Iaa
In other words, when we use the reference of the base class or the reference of the interface, the function pointed to is the ba.Show () function.
therefore:
((Iaa) b) .show (); // Why de.show is displayed -------- So ba.show is displayed
But here again because of the use of the new keyword, all when we use the reference of de to access the Show () function
The function no longer points to the ba method in the base class, but to its own new method Show ()
therefore:
b.show (); // why show ba.show ----------- so show de.show
Too many coincidences!
------------------------------------- I don't know if it's right, but the analysis is not executed ---_ -"- |
|