问题描述
可能显示的文件:结果
的结果
的
所以我在C#中写我的第一个真正的计划。该计划将刮去从4个不同网站的数据。我的计划是有一个父类,将是这样的:
So I'm writing my first real program in C#. The program will scrape data from four different websites. My plan is to have one parent class that will look like this:
class Scraper
{
string scrapeDate(url);
string scrapeTime(url);
//&c.
}
然后我将继承它四节课。
Then I will have four classes that inherit from it.
另一个选择是让刮板
的接口,并有四节课实现它。
The other option is to make Scraper
an interface and have four classes implementing it.
什么是这些方法之间的区别?
What are the differences between these approaches?
推荐答案
类继承表示是的关系,例如,一个罐
是车
。如果您的情况不至少满足此,选择通过继承接口。
Class inheritance represents an "is-a" relationship, e.g., a Tank
is a Vehicle
. If your situation doesn't at least meet this, choose interface over inheritance.
如果所提出的基类没有为你的方法提供的默认实现,这将是另一个之所以选择在继承接口。
If the proposed base class doesn't provide default implementations for your methods, this would be another reason to choose interface over inheritance.
在C#中,你只能从一个类,但多个接口继承。这将是另一个理由选择在继承接口。
In C#, you can only inherit from one class but multiple interfaces. This would be yet another reason to choose interface over inheritance.
有此想法?
这篇关于继承与在C#中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!