المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How can I make a method abstract in a child class?



C# Programming
05-04-2009, 02:12 AM
public abstract class Base
{
public int GetInt()
{
return 1;
}
}

public abstract class AbstractChild : Base
{
public abstract int GetInt();
}

public class Child : AbstractChild
{
public override int GetInt()
{
return 2;
}
}

I want all implementers of AbstractChild to implement GetInt method. So, I decide to make the method abstract even though it's not originally abstract in the Base class. Then I implement it in the Child class. But when I execute the following code, it surprisingly returns 1:

Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1

Since b is of type Child, I would normally expect the overridden method in Child class to be called. Why is the method in base class being called? Am I misunderstanding something?