c# - C# 中的继承与接口(interface)

标签 c# inheritance interface

<分区>

Possible Duplicates:
Interface vs Base class
When should I choose inheritance over an interface when designing C# class libraries?

所以我正在用 C# 编写我的第一个真正的程序。该程序将从四个不同的网站抓取数据。我的计划是拥有一个如下所示的父类:

class Scraper
{
    string scrapeDate(url);
    string scrapeTime(url);
    //&c.
}

然后我将有四个继承自它的类。

另一种选择是使 Scraper 成为一个接口(interface),并有四个类实现它。

这些方法之间有什么区别?

最佳答案

类继承表示一种“is-a”关系,例如,TankVehicle。如果您的情况至少不符合这一点,请选择接口(interface)而不是继承。

如果提议的基类没有为您的方法提供默认实现,这将是选择接口(interface)而不是继承的另一个原因。

在C#中,只能继承一个类,不能继承多个接口(interface)。这将是选择接口(interface)而不是继承的另一个原因。

明白了吗?

关于c# - C# 中的继承与接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188827/

相关文章:

c# - 具有动态属性名称的 Linq 选择器

java - JPA 继承不是 DRY

c# - 继承通用抽象

java - 实现线程接口(interface)

java - 使用通用接口(interface)进行类转换而不发出警告

c# - 有没有办法创建 ADO.NET 连接并忽略环境事务?

c# - 序列化从 CLR 类型继承的 IronPython 对象

c# - 无法从同一台服务器访问网站

c++ - 结构继承 : implicitly public or private?

php - 交响乐 : is there a best practice about the directories' name including traits and interfaces?