如何决定选择抽象类或接口 [英] How to decide to choose Abstract class or an Interface

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

问题描述

大家好,

我是使用Microsoft技术的软件开发人员,并且在C#,VB.NET,Win Forms& Windows Service应用程序开发.我在提出一个上述问题,我知道我们可以将方法的具体实现放在抽象类中,但不能在接口中实现,而抽象类来自实现中的继承链和接口,但是我的问题是我们如何选择其中之一,优势是什么?接口抽象类之间的缺点.此处:

 IMyInterface {
     void  MethodA();
     void  MethodM();
}

 MyInterfaceImplementation:IMyInterface {
    公共 无效 MethodA(){/*   ... */} // 不要喜欢这个语法
    无效 IMyInterface.MethodB(){/*   ... */} // 不要偏爱这种语法
}

//  ...为什么会有这样的偏好?让我们看看:

MyInterfaceImplementation impl =  MyInterfaceImplementation();
IMyInterface intrfHandle = impl; // 到目前为止一切顺利
intrfHandle.MethodA(); intrfHandle.MethodB(); // 预期用途

impl.MethodA(); // 允许,但不是很好:意外使用
// 将无法编译:
//  impl.MethodB(); //无法从类句柄访问,需要接口句柄 



上一次(注释掉)调用不会编译的事实确实很好,它有助于将接口表示的类实例与实现隔离开来,这也是首选接口接口"的另一个原因.

—SA


Hello All,

I am a software developer on Microsoft technologies and having 4 years of experience in C#, VB.NET, Win forms & Windows Service Application development. I am raising a the above question and I know that we can place the concrete implementation of methods in abstract class but we can''t implement in an interface and abstract class comes in inheriting chain and Interface in implementation but my question is how we choose one of them and what is the advantage & disadvantage between interface abstract class.

解决方案

I could have written it myself but then why to go along all the way when the same thing is already told by lots earlier, look here: Google search result for the same[^]

Though to start with, one of the reasons:
If you are in a situation where your Interface method implementation is same for all the inherting classes then you don''t need interface, instead Abstract class should had been used.
When you want something to be followed by every class (must have) and there implementation would differ or not defined then we go for Interface.


Rule of thumb:
If most of the derived classes really would take advantage of inheriting common behaviour then use the abstract base class, otherwise choose the interface (if you''re building a library then use the interface).
:)


The best answer here is the one by C Pallini.

It needs one subtle recommendation: do not use abstract classes where interface can be used.

It looks like the inference from the rule of thumb offered by C Pallini, but it is not.
Sometimes, you need a common behavior for derived classes which you can use polymorphous container. Interface cannot be used here, because late binding is involved. At the same time, you can use the same very abstract class to express a common interface to all of the classes to be passed, say, as a parameter to some other class not related to your hierarchy.

Event if the public/internal set of features matches your goal — don''t do it! Why? In this way you would cut yourself from extra flexibility: ability to create another class not related to your existing hierarchy implementing the same interface.

One could also create a "poor virtual" abstract class (a class with method-only members; all of them being abstract), as a logical equivalent of interface (this is how C++ interfaces are implemented). Same thing: don''t do it! Interfaces are interfaces: they add extra isolation from implementation. Consider the following example:

IMyInterface {
    void MethodA();
    void MethodM();
}

class MyInterfaceImplementation : IMyInterface {
    public void MethodA() { /*...*/ } //DO NOT prefer this syntax
    void IMyInterface.MethodB() { /*...*/ } //DO prefer this syntax
}

//... why such preference? Let's see:

MyInterfaceImplementation impl = new MyInterfaceImplementation();
IMyInterface intrfHandle = impl; //so far so good
intrfHandle.MethodA(); intrfHandle.MethodB(); //intended use

impl.MethodA(); //allowed, but not for good: unintended use
//will not compile:
//impl.MethodB(); //not accessible from class handle, needs interface handle



The fact that last (commented out) call will not compile is really good, it help isolation of class instances represented by interfaces from implementations, another reason to prefer "interface for interfacing".

—SA


这篇关于如何决定选择抽象类或接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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