假设我有一个抽象基类,比如说:
abstract class Item : IDisplayable
{
public int Id { get; set; }
public string Name { get; set; }
public abstract void Print();
}
我有一个继承自它的类,如下:
class Chair: Item
{
public int NumberOfLegs {get;set;}
public void Print()
{
Console.WriteLine("Here is a simple interface implementation");
}
}
interface IDisplayable
{
void Print();
}
子类并没有明确说明它也实现了该接口,但通过简单的继承它将实现该接口。如果我们在子类中显式添加接口,程序将以相同的方式运行(至少在我的简单示例中是这样)。显式实现接口是一个好主意还是坏主意,还是纯粹是个人喜好的问题?
Item
至少需要将Print()
声明为abstract
,如果没有实现它,它将无法编译。 - Jesse C. SlicerChair.Print
标记为override
? - Eric Lippert