为什么抽象类和接口都存在于C#中?

时间:2022-09-02 11:38:08

Why do both the abstract class and interface exist in C# if we can achieve the interface feature by making all the members in the class as abstract.

如果我们可以通过将类中的所有成员都作为抽象来实现接口功能,那么为什么抽象类和接口都存在于C#中。

Is it because:

是因为:

  1. Interface exists to have multiple inheritance
  2. 接口存在多重继承

  3. It makes sense to have interface because object's CAN-DO feature should be placed in an interface rather base abstract class.
  4. 有接口是有意义的,因为对象的CAN-DO功能应放在接口而不是基本抽象类中。

Please clarify

11 个解决方案

#1


26  

Well, an abstract class can specify some implemetation, but usually not all of it. (Having said which, it's perfectly possible to provide an abstract class with no abstract members, but plenty of virtual ones which with "no-op" implementations). An interface provides no implementation, merely a contract.

好吧,抽象类可以指定一些实现,但通常不是全部。 (已经说过,完全有可能提供一个没有抽象成员的抽象类,但是很多虚拟的抽象类都带有“no-op”实现)。接口不提供实现,仅提供合同。

You could certainly argue that if multiple inheritance of classes were permitted, interfaces would be largely pointless.

你当然可以争辩说,如果允许类的多重继承,接口将毫无意义。

Personally I don't get hung up on the whole "is-a" vs "can-do" distinction for inheritance. It never gives me as good an intuition about what to do as just playing around with different ideas and seeing which ones feel the most flexible. (Then again, I'm very much a "favour composition over inheritance" guy...)

就个人而言,我并没有因为继承而在整个“is-a”vs“can-do”区别。它从来没有给我一个关于做什么的直觉,只是玩弄不同的想法,看看哪些人觉得最灵活。 (再说一遍,我非常喜欢“对继承的偏爱”......)

EDIT: Just as the most convenient way of rebutting lbushkin's third point in his comment... you can override an abstract method with a non-virtual one (in terms of not being able to override it further) by sealing it:

编辑:正如在他的评论中反驳lbushkin第三点的最方便的方法...你可以通过密封它来覆盖一个非虚拟的抽象方法(就无法进一步覆盖它而言):

public abstract class AbstractBase
{
    public abstract void Foo();
}

public class Derived : AbstractBase
{
    public sealed override void Foo() {}
}

Classes deriving from Derived cannot override Foo any further.

派生自Derived的类不能再覆盖Foo。

I'm not in any way suggesting I want multiple inheritance of implementation - but if we did have it (along with its complexity) then an abstract class which just contained abstract methods would accomplish almost everything that an interface does. (There's the matter of explicit interface implementation, but that's all I can think of at the moment.)

我并不以任何方式暗示我想要实现多重继承 - 但如果我们确实拥有它(以及它的复杂性)那么一个只包含抽象方法的抽象类几乎可以完成接口所做的一切。 (这是显式接口实现的问题,但这是我现在所能想到的。)

#2


14  

It's not a trivial question, it's a very good question and one I always ask any candidates I interview.
In a nutshell - an abstract base class defines a type hierarchy whereas an interface defines a contract.

这不是一个微不足道的问题,这是一个非常好的问题,我总是问我采访的任何候选人。简而言之 - 抽象基类定义了类型层次结构,而接口定义了契约。

You can see it as is a vs implements a.
i.e Account could be an abstract base account because you could have a CheckingAccount, a SavingsAccount, etc all which derive from the abstract base class Account. Abstract base classes may also contain non abstract methods, properties and fields just like any normal class. However interfaces only contain abstract methods and properties that must be implemented.

你可以看到它是一个vs实现一个。即帐户可以是一个抽象的基本帐户,因为您可以拥有一个CheckingAccount,SavingsAccount等,这些都来自抽象基类Account。抽象基类也可以包含非抽象方法,属性和字段,就像任何普通类一样。但是,接口仅包含必须实现的抽象方法和属性。

c# let's you derive from one base class only - single inheritance just like java. However you can implement as many interfaces as you like - this is because an interface is just a contract which your class promises to implement.

c#let你只从一个基类派生 - 单继承就像java一样。但是,您可以根据需要实现任意数量的接口 - 这是因为接口只是您的类承诺实现的合同。

So if I had a class SourceFile then my class could choose to implement ISourceControl which says 'I faithfully promise to implement the methods and properties that ISourceControl requires'

所以,如果我有一个SourceFile类,那么我的类可以选择实现ISourceControl,它说'我忠实地承诺实现ISourceControl所需的方法和属性'

This is a big area and probably worthy of a better post than the one I've given however I'm short on time but I hope that helps!

这是一个很大的领域,可能比我给出的更好,但我的时间很短,但我希望有所帮助!

#3


7  

They both exist because they are both very different things. Abstract classes permit implementation and interfaces do not. An interface is very handy as it allows me to to say something about the type I am building (it is serializable, it is edible, etc.) but it does not allow me to define any implementation for the members I define.

它们都存在,因为它们都是截然不同的东西。抽象类允许实现,而接口则不允许。界面非常方便,因为它允许我说出我正在构建的类型(它是可序列化的,可食用的等),但它不允许我为我定义的成员定义任何实现。

An abstract class is more powerful that an interface in the sense that it allows me to create an inheritance interface via abstract and virtual members but also provide some sort of default or base implementation if I so choose. As Spiderman knows, however, with that great power comes great responsibility as an abstract class is more architecturally brittle.

抽象类比某个界面更强大,它允许我通过抽象和虚拟成员创建继承接口,但如果我选择的话,还提供某种默认或基本实现。然而,正如蜘蛛侠所知道的那样,由于抽象类在架构上更加脆弱,因此强大的力量带来了巨大的责任。

Side Note: Something interesting to note is that Vance Morrrison (of the CLR team) has speculated about adding default method implementations to interfaces in a future version of the CLR. This would greatly blur the distinction between an interface and an abstract class. See this video for details.

附注:值得注意的是,Vance Morrrison(CLR团队)推测在未来版本的CLR中将默认方法实现添加到接口。这将极大地模糊接口和抽象类之间的区别。有关详情,请参阅此视频。

#4


2  

One important reason both mechanisms exist because c#.NET only allows single inheritance, not multiple inheritance like C++. The class inheritance allows you to inherit implementation from only one place; everything else must be accomplished by implementing interfaces.

这两种机制存在的一个重要原因是因为c#.NET只允许单继承,而不是像C ++那样的多重继承。类继承允许您仅从一个地方继承实现;其他一切必须通过实现接口来完成。

For example, let's suppose I create a class, like Car and I subclass into three subclasses, RearWheelDrive, FrontWheelDrive, and AllWheelDrive. Now I decide that I need to cut my classes along a different "axis," like those with push-button starters and those without. I want all pushbutton start cars to have a "PushStartButton()" method and non-pushbutton cars to have a "TurnKey()" method and I want to be able to treat Car objects (with regard to starting them) irrespective of which subclass they are. I can define interfaces that my classes can implement, such as IPushButtonStart and IKeyedIgnition, so I have a common way to deal with my objects that differ in a way that is independent of the single base class from which each derives.

例如,假设我创建了一个类,如Car和I子类,分为三个子类:RearWheelDrive,FrontWheelDrive和AllWheelDrive。现在我决定我需要沿着不同的“轴”切割我的类,就像那些带有按钮启动器和没有按钮的那些。我希望所有按钮启动汽车都有一个“PushStartButton()”方法和非按钮汽车有一个“TurnKey()”方法,我希望能够处理Car对象(关于启动它们),而不管哪个子类他们是。我可以定义我的类可以实现的接口,例如IPushButtonStart和IKeyedIgnition,所以我有一种常见的方法来处理我的对象,这些对象的方式与每个派生的单个基类无关。

#5


1  

You gave a good answer already. I think your second answer is the real reason. If I wanted to make an object Compareable I shouldn't have to derive from a Comparable base class. if you think of all the interfaces think of all the permutations you'd beed to handle the basic interfaces like IComparable.

你已经给出了一个很好的答案。我认为你的第二个答案是真正的原因。如果我想创建一个对象Compareable,我不应该从Comparable基类派生。如果你认为所有接口都考虑了你想要处理IComparable等基本接口的所有排列。

Interfaces let us define a contract around the publicly exposed behavior an object provides. Abstract classes let you define both behavior and implementation, which is a very different thing.

接口让我们围绕对象提供的公开暴露行为定义合同。抽象类允许您定义行为和实现,这是一个非常不同的事情。

#6


1  

Interfaces exist to provide a class without any implementation whatsoever, so that .NET can provide support for safe and functional multiple inheritance in a managed environment.

存在接口以提供没有任何实现的类,因此.NET可以在托管环境中提供对安全和功能多重继承的支持。

#7


0  

An Interface defines a contract that an implementing class must fulfil; it is a way of stating that "this does that". An Abstract Class is a partial implementation of a class which is by definition incomplete, and which needs a derviation to be completed. They're very different things.

接口定义了实现类必须满足的合同;这是一种表明“这样做”的方式。抽象类是类的部分实现,根据定义它是不完整的,并且需要完成衍生。他们是非常不同的东西。

#8


0  

An abstract class can have an implementation while an interface just allows you to create a contract that implementers have to follow. With abstract classes you can provide a common behavior to their sub classes witch you can't with interfaces.

抽象类可以具有实现,而界面只允许您创建实现者必须遵循的契约。使用抽象类,您可以为子类提供一种常见的行为,而不能使用接口。

#9


0  

They serve two distinctly different purposes.

它们有两个截然不同的用途。

Abstract classes provide a way to have a an object inherit from a defined contract, as well as allowing behavior to be specified in the base class. This, from a theoretical standpoint, provides an IS-A relationship, in that the concrete class IS-A specific type of the base class.

抽象类提供了一种方法,使对象从定义的契约继承,并允许在基类中指定行为。从理论的角度来看,这提供了一种IS-A关系,即具体类IS-A特定类型的基类。

Interfaces allow classes to define a (or more than one) contract which they will fulfill. They allow for a ACTS-AS or "can be used as an" type of relationship, as opposed to direct inheritance. This is why, typically, interfaces will use an adjective as they're name (IDisposable) instead of a noun.

接口允许类定义它们将实现的(或多个)合同。它们允许ACTS-AS或“可以用作”类型的关系,而不是直接继承。这就是为什么通常接口会使用形容词,因为它们是名称(IDisposable)而不是名词。

#10


0  

An interface is used for what a class can do, but it is also used to hide some of things that a class can do.

接口用于类可以执行的操作,但它也用于隐藏类可以执行的操作。

For example the IEnumerable<T> interface describes that a class can iterate through it's members, but it's also limits the access to this single ability. A List<T> can also access the items by index, but when you access it through the IEnumerable<T> interface, you only know about it's ability to iterate the members.

例如,IEnumerable 接口描述了一个类可以遍历它的成员,但它也限制了对这个单一异能的访问。 List 也可以通过索引访问项目,但是当您通过IEnumerable 接口访问它时,您只知道它能够迭代成员。

If a method accepts the IEnumerable<T> interface as a parameter, that means that it's only interrested in the ability to iterate through the members. You can use several different classes with this ability (like a List<T> or an array T[]) without the need for one method for each class.

如果一个方法接受IEnumerable 接口作为参数,这意味着它只对迭代成员的能力感兴趣。您可以使用具有此功能的多个不同类(如List 或数组T []),而无需为每个类使用一个方法。

Not only can a method accept several different classes that implement an interface, you can create new classes that implement the interface and the method will happily accept those too.

方法不仅可以接受实现接口的几个不同的类,还可以创建实现接口的新类,并且方法也会愉快地接受它们。

#11


0  

Consider having a class Fruit that is derived by two children(Apple & Banana) as shown below:

考虑使用由两个孩子(Apple和Banana)派生的类Fruit,如下所示:

class Fruit
{
    public virtual string GetColor()
    {
        return string.Empty;
    }
}

class Apple : Fruit
{
    public override string GetColor()
    {
        return "Red";
    }
}

class Banana : Fruit
{
    public override string GetColor()
    {
        return "Yellow";
    }
}

We have an existing interface ICloneable in C#. This interface has a single method as shown below, a class that implements this interface guarantees that it can be cloned:

我们在C#中有一个现有的IClone接口。此接口具有如下所示的单个方法,实现此接口的类保证可以克隆它:

public interface ICloneable
{
    object Clone();
}

Now if I want to make my Apple class(not Banana class) clonable, I can simpley implement ICloneable like this:

现在,如果我想让我的Apple类(不是Banana类)克隆,我可以像这样简单地实现ICloneable:

 class Apple : Fruit , ICloneable
{
    public object Clone()
    {
        // add your code here
    }

    public override string GetColor()
    {
        return "Red";
    }
}

Now considering your argument of pure abstract class, if C# had a pure abstract class say Clonable instead of interface IClonable like this:

现在考虑你的纯抽象类的论点,如果C#有一个纯抽象类,请说Clonable而不是接口IClonable,如下所示:

abstract class Clonable
{
    public abstract object Clone();
}

Could you now make your Apple class clonable by inheriting the abstract Clonable instead of IClonable? like this:

您现在可以通过继承抽象Clonable而不是IClonable来使您的Apple类可克隆吗?像这样:

// Error: Class 'Apple' cannot have multiple base classes: 'Fruit' & 'Clonable'
class Apple : Fruit, Clonable
{
    public object Clone()
    {
        // add your code here
    }

    public override string GetColor()
    {
        return "Red";
    }
}

No, you can't, because a class cannot derive from multiple classes.

不,你不能,因为一个类不能从多个类派生。

#1


26  

Well, an abstract class can specify some implemetation, but usually not all of it. (Having said which, it's perfectly possible to provide an abstract class with no abstract members, but plenty of virtual ones which with "no-op" implementations). An interface provides no implementation, merely a contract.

好吧,抽象类可以指定一些实现,但通常不是全部。 (已经说过,完全有可能提供一个没有抽象成员的抽象类,但是很多虚拟的抽象类都带有“no-op”实现)。接口不提供实现,仅提供合同。

You could certainly argue that if multiple inheritance of classes were permitted, interfaces would be largely pointless.

你当然可以争辩说,如果允许类的多重继承,接口将毫无意义。

Personally I don't get hung up on the whole "is-a" vs "can-do" distinction for inheritance. It never gives me as good an intuition about what to do as just playing around with different ideas and seeing which ones feel the most flexible. (Then again, I'm very much a "favour composition over inheritance" guy...)

就个人而言,我并没有因为继承而在整个“is-a”vs“can-do”区别。它从来没有给我一个关于做什么的直觉,只是玩弄不同的想法,看看哪些人觉得最灵活。 (再说一遍,我非常喜欢“对继承的偏爱”......)

EDIT: Just as the most convenient way of rebutting lbushkin's third point in his comment... you can override an abstract method with a non-virtual one (in terms of not being able to override it further) by sealing it:

编辑:正如在他的评论中反驳lbushkin第三点的最方便的方法...你可以通过密封它来覆盖一个非虚拟的抽象方法(就无法进一步覆盖它而言):

public abstract class AbstractBase
{
    public abstract void Foo();
}

public class Derived : AbstractBase
{
    public sealed override void Foo() {}
}

Classes deriving from Derived cannot override Foo any further.

派生自Derived的类不能再覆盖Foo。

I'm not in any way suggesting I want multiple inheritance of implementation - but if we did have it (along with its complexity) then an abstract class which just contained abstract methods would accomplish almost everything that an interface does. (There's the matter of explicit interface implementation, but that's all I can think of at the moment.)

我并不以任何方式暗示我想要实现多重继承 - 但如果我们确实拥有它(以及它的复杂性)那么一个只包含抽象方法的抽象类几乎可以完成接口所做的一切。 (这是显式接口实现的问题,但这是我现在所能想到的。)

#2


14  

It's not a trivial question, it's a very good question and one I always ask any candidates I interview.
In a nutshell - an abstract base class defines a type hierarchy whereas an interface defines a contract.

这不是一个微不足道的问题,这是一个非常好的问题,我总是问我采访的任何候选人。简而言之 - 抽象基类定义了类型层次结构,而接口定义了契约。

You can see it as is a vs implements a.
i.e Account could be an abstract base account because you could have a CheckingAccount, a SavingsAccount, etc all which derive from the abstract base class Account. Abstract base classes may also contain non abstract methods, properties and fields just like any normal class. However interfaces only contain abstract methods and properties that must be implemented.

你可以看到它是一个vs实现一个。即帐户可以是一个抽象的基本帐户,因为您可以拥有一个CheckingAccount,SavingsAccount等,这些都来自抽象基类Account。抽象基类也可以包含非抽象方法,属性和字段,就像任何普通类一样。但是,接口仅包含必须实现的抽象方法和属性。

c# let's you derive from one base class only - single inheritance just like java. However you can implement as many interfaces as you like - this is because an interface is just a contract which your class promises to implement.

c#let你只从一个基类派生 - 单继承就像java一样。但是,您可以根据需要实现任意数量的接口 - 这是因为接口只是您的类承诺实现的合同。

So if I had a class SourceFile then my class could choose to implement ISourceControl which says 'I faithfully promise to implement the methods and properties that ISourceControl requires'

所以,如果我有一个SourceFile类,那么我的类可以选择实现ISourceControl,它说'我忠实地承诺实现ISourceControl所需的方法和属性'

This is a big area and probably worthy of a better post than the one I've given however I'm short on time but I hope that helps!

这是一个很大的领域,可能比我给出的更好,但我的时间很短,但我希望有所帮助!

#3


7  

They both exist because they are both very different things. Abstract classes permit implementation and interfaces do not. An interface is very handy as it allows me to to say something about the type I am building (it is serializable, it is edible, etc.) but it does not allow me to define any implementation for the members I define.

它们都存在,因为它们都是截然不同的东西。抽象类允许实现,而接口则不允许。界面非常方便,因为它允许我说出我正在构建的类型(它是可序列化的,可食用的等),但它不允许我为我定义的成员定义任何实现。

An abstract class is more powerful that an interface in the sense that it allows me to create an inheritance interface via abstract and virtual members but also provide some sort of default or base implementation if I so choose. As Spiderman knows, however, with that great power comes great responsibility as an abstract class is more architecturally brittle.

抽象类比某个界面更强大,它允许我通过抽象和虚拟成员创建继承接口,但如果我选择的话,还提供某种默认或基本实现。然而,正如蜘蛛侠所知道的那样,由于抽象类在架构上更加脆弱,因此强大的力量带来了巨大的责任。

Side Note: Something interesting to note is that Vance Morrrison (of the CLR team) has speculated about adding default method implementations to interfaces in a future version of the CLR. This would greatly blur the distinction between an interface and an abstract class. See this video for details.

附注:值得注意的是,Vance Morrrison(CLR团队)推测在未来版本的CLR中将默认方法实现添加到接口。这将极大地模糊接口和抽象类之间的区别。有关详情,请参阅此视频。

#4


2  

One important reason both mechanisms exist because c#.NET only allows single inheritance, not multiple inheritance like C++. The class inheritance allows you to inherit implementation from only one place; everything else must be accomplished by implementing interfaces.

这两种机制存在的一个重要原因是因为c#.NET只允许单继承,而不是像C ++那样的多重继承。类继承允许您仅从一个地方继承实现;其他一切必须通过实现接口来完成。

For example, let's suppose I create a class, like Car and I subclass into three subclasses, RearWheelDrive, FrontWheelDrive, and AllWheelDrive. Now I decide that I need to cut my classes along a different "axis," like those with push-button starters and those without. I want all pushbutton start cars to have a "PushStartButton()" method and non-pushbutton cars to have a "TurnKey()" method and I want to be able to treat Car objects (with regard to starting them) irrespective of which subclass they are. I can define interfaces that my classes can implement, such as IPushButtonStart and IKeyedIgnition, so I have a common way to deal with my objects that differ in a way that is independent of the single base class from which each derives.

例如,假设我创建了一个类,如Car和I子类,分为三个子类:RearWheelDrive,FrontWheelDrive和AllWheelDrive。现在我决定我需要沿着不同的“轴”切割我的类,就像那些带有按钮启动器和没有按钮的那些。我希望所有按钮启动汽车都有一个“PushStartButton()”方法和非按钮汽车有一个“TurnKey()”方法,我希望能够处理Car对象(关于启动它们),而不管哪个子类他们是。我可以定义我的类可以实现的接口,例如IPushButtonStart和IKeyedIgnition,所以我有一种常见的方法来处理我的对象,这些对象的方式与每个派生的单个基类无关。

#5


1  

You gave a good answer already. I think your second answer is the real reason. If I wanted to make an object Compareable I shouldn't have to derive from a Comparable base class. if you think of all the interfaces think of all the permutations you'd beed to handle the basic interfaces like IComparable.

你已经给出了一个很好的答案。我认为你的第二个答案是真正的原因。如果我想创建一个对象Compareable,我不应该从Comparable基类派生。如果你认为所有接口都考虑了你想要处理IComparable等基本接口的所有排列。

Interfaces let us define a contract around the publicly exposed behavior an object provides. Abstract classes let you define both behavior and implementation, which is a very different thing.

接口让我们围绕对象提供的公开暴露行为定义合同。抽象类允许您定义行为和实现,这是一个非常不同的事情。

#6


1  

Interfaces exist to provide a class without any implementation whatsoever, so that .NET can provide support for safe and functional multiple inheritance in a managed environment.

存在接口以提供没有任何实现的类,因此.NET可以在托管环境中提供对安全和功能多重继承的支持。

#7


0  

An Interface defines a contract that an implementing class must fulfil; it is a way of stating that "this does that". An Abstract Class is a partial implementation of a class which is by definition incomplete, and which needs a derviation to be completed. They're very different things.

接口定义了实现类必须满足的合同;这是一种表明“这样做”的方式。抽象类是类的部分实现,根据定义它是不完整的,并且需要完成衍生。他们是非常不同的东西。

#8


0  

An abstract class can have an implementation while an interface just allows you to create a contract that implementers have to follow. With abstract classes you can provide a common behavior to their sub classes witch you can't with interfaces.

抽象类可以具有实现,而界面只允许您创建实现者必须遵循的契约。使用抽象类,您可以为子类提供一种常见的行为,而不能使用接口。

#9


0  

They serve two distinctly different purposes.

它们有两个截然不同的用途。

Abstract classes provide a way to have a an object inherit from a defined contract, as well as allowing behavior to be specified in the base class. This, from a theoretical standpoint, provides an IS-A relationship, in that the concrete class IS-A specific type of the base class.

抽象类提供了一种方法,使对象从定义的契约继承,并允许在基类中指定行为。从理论的角度来看,这提供了一种IS-A关系,即具体类IS-A特定类型的基类。

Interfaces allow classes to define a (or more than one) contract which they will fulfill. They allow for a ACTS-AS or "can be used as an" type of relationship, as opposed to direct inheritance. This is why, typically, interfaces will use an adjective as they're name (IDisposable) instead of a noun.

接口允许类定义它们将实现的(或多个)合同。它们允许ACTS-AS或“可以用作”类型的关系,而不是直接继承。这就是为什么通常接口会使用形容词,因为它们是名称(IDisposable)而不是名词。

#10


0  

An interface is used for what a class can do, but it is also used to hide some of things that a class can do.

接口用于类可以执行的操作,但它也用于隐藏类可以执行的操作。

For example the IEnumerable<T> interface describes that a class can iterate through it's members, but it's also limits the access to this single ability. A List<T> can also access the items by index, but when you access it through the IEnumerable<T> interface, you only know about it's ability to iterate the members.

例如,IEnumerable 接口描述了一个类可以遍历它的成员,但它也限制了对这个单一异能的访问。 List 也可以通过索引访问项目,但是当您通过IEnumerable 接口访问它时,您只知道它能够迭代成员。

If a method accepts the IEnumerable<T> interface as a parameter, that means that it's only interrested in the ability to iterate through the members. You can use several different classes with this ability (like a List<T> or an array T[]) without the need for one method for each class.

如果一个方法接受IEnumerable 接口作为参数,这意味着它只对迭代成员的能力感兴趣。您可以使用具有此功能的多个不同类(如List 或数组T []),而无需为每个类使用一个方法。

Not only can a method accept several different classes that implement an interface, you can create new classes that implement the interface and the method will happily accept those too.

方法不仅可以接受实现接口的几个不同的类,还可以创建实现接口的新类,并且方法也会愉快地接受它们。

#11


0  

Consider having a class Fruit that is derived by two children(Apple & Banana) as shown below:

考虑使用由两个孩子(Apple和Banana)派生的类Fruit,如下所示:

class Fruit
{
    public virtual string GetColor()
    {
        return string.Empty;
    }
}

class Apple : Fruit
{
    public override string GetColor()
    {
        return "Red";
    }
}

class Banana : Fruit
{
    public override string GetColor()
    {
        return "Yellow";
    }
}

We have an existing interface ICloneable in C#. This interface has a single method as shown below, a class that implements this interface guarantees that it can be cloned:

我们在C#中有一个现有的IClone接口。此接口具有如下所示的单个方法,实现此接口的类保证可以克隆它:

public interface ICloneable
{
    object Clone();
}

Now if I want to make my Apple class(not Banana class) clonable, I can simpley implement ICloneable like this:

现在,如果我想让我的Apple类(不是Banana类)克隆,我可以像这样简单地实现ICloneable:

 class Apple : Fruit , ICloneable
{
    public object Clone()
    {
        // add your code here
    }

    public override string GetColor()
    {
        return "Red";
    }
}

Now considering your argument of pure abstract class, if C# had a pure abstract class say Clonable instead of interface IClonable like this:

现在考虑你的纯抽象类的论点,如果C#有一个纯抽象类,请说Clonable而不是接口IClonable,如下所示:

abstract class Clonable
{
    public abstract object Clone();
}

Could you now make your Apple class clonable by inheriting the abstract Clonable instead of IClonable? like this:

您现在可以通过继承抽象Clonable而不是IClonable来使您的Apple类可克隆吗?像这样:

// Error: Class 'Apple' cannot have multiple base classes: 'Fruit' & 'Clonable'
class Apple : Fruit, Clonable
{
    public object Clone()
    {
        // add your code here
    }

    public override string GetColor()
    {
        return "Red";
    }
}

No, you can't, because a class cannot derive from multiple classes.

不,你不能,因为一个类不能从多个类派生。