抽象类和接口与示例有什么区别。 [英] What is difference between abstract class and interface with example.

查看:78
本文介绍了抽象类和接口与示例有什么区别。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象类界面之间有什么区别?哪一个和为什么一个比另一个更有益?

What is the difference between an abstract class and an interface? Where and why would one be more beneficial over the other?

推荐答案

Interface : should be used if you want to imply a rule on the components which may or may not be
related to each other

Abstract Class : should be used where you want to have some basic or default behavior or
implementation for components related to each other

Pros:
Interface:

    Allows multiple inheritance
    provides abstraction by not exposing what exact kind of object is being used in the context
    provides consistency by a specific signature of the contract


Abstract Class:

    faster then interface
    has flexibility in the implementation (you can implement it fully or partially)
    can be easily changed without breaking the derived classes


Cons:
Interface :

    Must implement all the contracts defined
    cannot have variables or delegates
    once defined cannot be changed without breaking all the classes


Abstract Class :

    cannot be instantiated
    does not support multiple inheritance


我认为如果你进行搜索,你会看到大量的文件来解释这些差异。但我会恢复它:



接口不能有任何方法的默认实现。抽象类可以。

一个类可以实现任何接口,但如果你已经有一个基类型的类,它将无法继承抽象类。



何时使用每一个?

一般情况下,我同时使用两者。

我创建一个界面,所以所有方法都可以在很多不同的方面重新实现方法(例如,有助于远程处理或为测试目的创建虚假实现)。

但是如果某些方法可能会被许多类以相同的方式实现,我会创建一个抽象已经实现了这种方法但是让其他方法完全虚拟的类。



样本?如果你看一下我最新文章的样本创建你自己的动画格式 [ ^ ]你会看到以下内容:



IFrameReader。此接口具有非通用的无类型版本,如果您具有正确的对象但在编译时不知道泛型类型(即,将参数作为对象传递),则可以使用该接口。

然后如果您在编译时知道正确的类型(例如ManagedBitmap< Rgb24>),它有一个可以使用的通用版本并避免强制转换。



但是,到那些想要实现FrameReader类并且不想浪费时间实现两个接口的人(因为无类型版本将简单地重定向到类型化版本),有一个FrameReader类已经实现了这两个接口。无类型(非泛型)版本简单重定向到类型化版本,它将这些类型化的方法保持为抽象。



因此,从FrameReader类继承你已经得到了实现了IFrameReader,但您仍然需要实现使IFrameReader< T>成立的方法。工作。



我希望这很清楚。
I think that if you do a search you will see tons of documents explaining the difference. But I will resume it as this:

Interfaces can't have a default implementation for any method. Abstract classes can.
A class can implement any interface, but if you already have a class with a base type, it will not be capable of inheriting from an abstract class.

When to use each one?
In general I use both.
I create an interface so all methods can be reimplemented in many different ways (that helps in remoting, for example, or to create fake implementations for testing purposes).
But if some method is probably going to be implemented in the same way by many classes, I create an abstract class that already implement such method but let other methods be fully virtual.

A sample? If you look at the sample of my latest article Creating your own animation format[^] you will see the following:

IFrameReader. This interface has a non-generic untyped version that can be used if you have the right objects but don't know the generic type at compile time (that is, you pass parameters as objects).
Then it has a generic version that you can use and avoid casts if you know the right types at compile time (the ManagedBitmap<Rgb24> for example).

But, to those who want to implement a FrameReader class and don't want to lose time implementing both interfaces (as the untyped version will simple redirect to the typed version), there is the FrameReader class that already implements both interfaces. The untyped (non-generic) version simple redirects to the typed version, which keep those typed methods as abstract.

So, inheriting from the FrameReader class you get an already implemented IFrameReader but you still need to implement the methods that make the IFrameReader<T> work.

I hope this is clear enough.


参考下面的文章



抽象类与接口 [ ^ ]
Refer below article

Abstract Class versus Interface[^]


这篇关于抽象类和接口与示例有什么区别。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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