接口与基类 [英] Interface vs Base class

查看:203
本文介绍了接口与基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我何时应该使用接口,何时应该使用基类?



如果我不想实际定义方法的基本实现,它应该始终是一个接口吗?



<如果我有狗和猫类。为什么我要实现IPet而不是PetBase?我可以理解有ISheds或IBarks(IMakesNoise?)的接口,因为那些可以放在宠物的宠物基础上,但我不明白哪个用于通用宠物。

解决方案


让我们举一个Dog和Cat类的例子,让我们用C#来说明:



狗和猫都是动物,特别是四足动物的哺乳动物(动物过于笼统)。让我们假设你有两个抽象类Mammal:

 公共抽象类哺乳动物

这个基类可能有默认方法,例如:




  • Feed

  • Mate



全部其中一种是在两个物种之间具有或多或少相同实施的行为。要定义这个,你将有:

 公共类Dog:Mammal 
公共类Cat :哺乳动物

现在让我们假设还有其他哺乳动物,我们通常会在动物园看到:

 公共类长颈鹿:哺乳动物
公共类Rhinoceros:哺乳动物
公共类Hippopotamus :哺乳动物

这仍然有效,因为功能的核心 Feed () Mate()仍然是相同的。



然而,长颈鹿,犀牛和河马并不是你可以养宠物的动物。这就是界面有用的地方:

 公共接口IPettable 
{
IList< Trick>技巧{获得; set;}
void Bathe();
void Train(Trick t);
}

上述合约的实施在猫与狗之间是不一样的;将他们的实现放在一个抽象类中继承将是一个坏主意。



您的狗和猫定义现在应该如下所示:

 公共类狗:哺乳动物,IPettable 
公共类猫:哺乳动物,IPettable

理论上你可以从更高的基类覆盖它们,但实际上一个接口允许你只需要在类中添加你需要的东西而不需要继承。



因此,因为您通常只能从一个抽象类继承(在大多数静态类型的OO语言中,例外包括C ++)但是能够实现多个接口,它允许您在严格的中构造对象根据需要基础。


When should I use an interface and when should I use a base class?

Should it always be an interface if I don't want to actually define a base implementation of the methods?

If I have a Dog and Cat class. Why would I want to implement IPet instead of PetBase? I can understand having interfaces for ISheds or IBarks (IMakesNoise?), because those can be placed on a pet by pet basis, but I don't understand which to use for a generic Pet.

解决方案

Let's take your example of a Dog and a Cat class, and let's illustrate using C#:

Both a dog and a cat are animals, specifically, quadruped mammals (animals are waaay too general). Let us assume that you have an abstract class Mammal, for both of them:

public abstract class Mammal

This base class will probably have default methods such as:

  • Feed
  • Mate

All of which are behavior that have more or less the same implementation between either species. To define this you will have:

public class Dog : Mammal
public class Cat : Mammal

Now let's suppose there are other mammals, which we will usually see in a zoo:

public class Giraffe : Mammal
public class Rhinoceros : Mammal
public class Hippopotamus : Mammal

This will still be valid because at the core of the functionality Feed() and Mate() will still be the same.

However, giraffes, rhinoceros, and hippos are not exactly animals that you can make pets out of. That's where an interface will be useful:

public interface IPettable
{
    IList<Trick> Tricks{get; set;}
    void Bathe();
    void Train(Trick t);
}

The implementation for the above contract will not be the same between a cat and dog; putting their implementations in an abstract class to inherit will be a bad idea.

Your Dog and Cat definitions should now look like:

public class Dog : Mammal, IPettable
public class Cat : Mammal, IPettable

Theoretically you can override them from a higher base class, but essentially an interface allows you to add on only the things you need into a class without the need for inheritance.

Consequently, because you can usually only inherit from one abstract class (in most statically typed OO languages that is... exceptions include C++) but be able to implement multiple interfaces, it allows you to construct objects in a strictly as required basis.

这篇关于接口与基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆