public class Father
{
// static string TYPE = "FATHER";
public Father()
{
//Console.WriteLine("ctor");
}
public virtual void Print()
{
Console.WriteLine("I'm father");
}
}
public class Son:Father
{
public override void Print()
{
base.Print();
Console.WriteLine("I'm son");
}
}
As we konw, if we call Son.Print(),It'll print out "I'm father" and "I'm son".And Father.Print() is an instance method ,we need to create an instance first.So that's the question,who creates it?Obviously,not me... Or Son owns two Print methods in the methodtable.One of them can be accessed by Father,anthor can be accessed by itself? Which one is right?Or neither is right?Please tell me!Thanks!
Sonis printing both statements because in this case aSonis aFather. While that holds true for a segment of the population, it's certainly not universally true as implied by this inheritance hierarchy.SonandFathershould be interfaces, not inheriting classes. EachPersonentity should implement the appropriate interfaces. This is a great example of how not to use inheritance. (Though I'm sure there's a good "inheritance" joke in here somewhere regarding a father and son...)Shape,Square, andTriangleas everyone can understand that squares are shapes and triangles are shapes but triangles and squares are different things.