在哪里使用接口和抽象类 [英] Where to use interface and abstract class

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

问题描述

我必须知道特别使用的接口和抽象类

I have to know particular used of interface and abstract class

推荐答案

这真的很难谷歌。 界面与抽象类 [ ^ ]



这也是转贴的问题。请不要多次发布同一个问题。如果您对问题有更新,请使用改进问题小部件来更新现有问题,而不是提交新问题。
Is it really that hard to google. Interface vs Abstract class[^]

This is also a reposted question. Please don''t post the same question multiple times. If you have an update to a question, use the improve question widget to update your existing question instead of submitting a new one.


从技术上讲,这一切都很清楚,但做出设计决策是不是一件简单的事情。请看这些过去的答案:

当我们使用抽象时,当我们使用界面时......? [ ^ ],

抽象类和接口之间的区别,如果它们具有相同的no方法和var [ ^ ],

如何决定选择抽象类或接口 [ ^ ],

接口和多态性 [ ^ ]。



-SA
Technically it''s all clear, but making design decisions is not a simple thing. Please see these past answers:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

—SA


这两个概念是不同的,但相关。

让我们从一个接口开始。

C#只允许你派生一个新的类表单单个类:

The two concepts are different, but related.
Let''s start with an Interface.
C# will only allow you to derive a new class form a single class:
public class MyBaseClass {}
public class MyDerivedClass : MyBaseClass {}

您无法从两个或更多个派生类具体类:

You cannot derive a class from two or more concrete classes:

public class MyBaseClass1 {}
public class MyBaseClass2 {}
public class MyDerivedClass : MyBaseClass1, MyBaseClass2 {}

那么如何添加分组以便您可以将课程视为俱乐部的成员?例如,你怎么说这个新类是基于一个控件,但它也知道如何像一个项目列表?



答案是使用接口。这列出了俱乐部的规则(它定义了你必须实现哪些属性和方法才能加入),并且不以任何方式提供任何帮助或阻碍你的代码。在上面的示例中,IEnumerable接口要求您实现 GetEnumerator 方法,并且当您确实允许所有俱乐部会员资格时 - 例如,您的班级现在可以显示在foreach循环中。

So how do you add a grouping so that you can treat a class as a member of a club? For example, how do you say "this new class is based on a Control, but it also knows how to act like a List of items"?

The answer is to use an Interface. This lays out the rules of the "Club" (it defines which properties and methods you must implement in order to join) and does not in any way provide an code at all to either help or hinder you. In the example above, the IEnumerable Interface requires that you implement the GetEnumerator method, and when you do allows you all the priveliges ofthe club membership - for example, your class can now appear in a foreach loop.

    MyClass mc = new MyClass();
    foreach (string s in mc)
        {
        }
    }

public class MyClass : IEnumerable<string>
    {
    public IEnumerator GetEnumerator()
        {
        throw new NotImplementedException();
        }
    }







抽象类类似在某种程度上,但它本身就是一个类,所以你可以从它派生出来,但你不能同时从任何其他类派生出来。它可以像普通类一样实现属性和方法,但它并不是必须的。抽象类和普通(或具体)类之间的最大区别在于,在任何情况下都不能创建抽象类的实例 - 您可以声明引用变量,但是您只能创建派生类的实例。

例如,你可以有一个抽象类Fruit,它是Apple,Pear和Orange类的基础。




An abstract class is similar in a way, but it is a class in itself so you can derive from it, but you can''t derive from any other class at the same time. It can implement properties and methods in the same way as a "normal" class, but it doesn''t have to. The big difference between an abstract class and a "normal" (or concrete) class is that you cannot under any circumstances create an instance of the abstract class - you can declare a reference variable, but you can only ever create instances of derived classes.
For example, you could have an abstract class "Fruit" which is the base for the "Apple", "Pear", and "Orange" classes.

Fruit f = new Apple();
Orange o = new Orange();
f = o;

都是合法的。

但你不能说:

Are all legal.
But you can''t say:

Apple a = new Apple();
Orange o = new Orange();
o = a;

因为它们都是水果,但它们不是一样的水果。



你用一个摘要class创建一个没有实例的类 - 但是将实例组合在一起,并允许您将任何实例类传递给期望抽象基类的方法。



在现实世界中,您可以定义一个抽象类OutputDevice,它表示它需要一个带有字符串的Write方法,并定义一个从抽象派生的FileOutputDevice和TextBoxOutputDevice。如果两个具体类都实现了Write方法,则可以编写一个方法,该方法将OutputDevice作为teh参数并将其内容打印到该方法。它将在没有更改代码的情况下工作。您可以添加一个ConsoleOutputDevice类,该方法仍然无需更改即可工作!

Because they are both Fruit, but they aren''t the same fruit.

You use an abstract class to create a class that has no instances - but groups together instances, and allows you to pass any of the instance classes through to a method which expects the abstract base class.

In the real world, you could define an abstract class "OutputDevice" which says it wants a Write method which takes a string, and define a FileOutputDevice and a TextBoxOutputDevice which are derived from the abstract. Provided both concrete classes implement the Write method, you can write a single method which takes an OutputDevice as teh parameter and prints the contents of a book to it. It will the=n work without changes to the code. And you could add a ConsoleOutputDevice class and the method still needs no changes to work!


这篇关于在哪里使用接口和抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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