抽象类和接口 [英] Abstract class and interface

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

问题描述

关于抽象类和接口的一些混淆。

抽象类与所有方法抽象和接口有什么区别?

例如

Some confusion about abstract class and Interface.
What is difference between a abstract class with all method abstract and Interface?
e.g

Interface IData
{
void Save();
}
abstract class AData
{
public abstratct void Save();
}





两者有什么区别?



接口如何比抽象类更安全?

为什么依赖注入始终优先使用接口而不是抽象类?

而在构造函数DI注入器中我们可以传递相同的抽象类作为接口的功能。

例如



What is difference between Both?

How Interface is more secure than abstract class?
Why dependency Injection always preferred to use Interface not abstract class ?
while in constructor DI injector we can pass abstract class which is same functionality as Interface.
e.g

--===For Interface
Interface IData
{
void Save();
}
public class Sql:IData
{
}
public class Oracle:IData
{
}
public class Client
{
IData _data=null;
Public Client(IData data)
{
_data=data;
}
}
--===For abstarct class
abstract class AData
{
public abstratct void Save();
}
public class ABSql:AData
{
}
public class ABOracle:AData
{
}
public class Client
{
AData _data=null;
Public Client(AData data)
{
_data=data;
}
}



两者有什么区别?


What is difference between both?

推荐答案

First of所有,几乎所有差异问题都是无效的。如果你不明白为什么,试着告诉我们苹果和苹果之间的区别。 :-)



只需学习这两个概念,研究所有细节,用例等等。



真的有很多东西。关于他们之间的设计选择和一些动机,请阅读我过去关于这个主题的答案:

为什么我们需要为接口创建实例而不是实现? [ ^ ],

对接口的怀疑 [ ^ ],

如果abstact类实现了接口那么是否需要在抽象类中实现接口方法。 [ ^ ],

没有过载和覆盖的多态性可能 [ ^ ],

接口和多态性 [ ^ ],

如何决定选择抽象类或界面 [ ^ ],

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

当我们使用抽象时和使用界面时?? [ ^ ]。



-SA
First of all, nearly all "difference" questions are invalid. If you don't understand why, try to tell us the difference between apple and Apple. :-)

Simply learn both concepts, study all the detail, use cases, and so on.

There is really a lot to it. On the design choice between them and some motivation, please read my past answers on the topic:
Why we need to create instance for interface not for implementation?[^],
Doubts on Interfaces[^],
If abstact class implements the interface then is there any need to implement interface method in abstract class.[^],
POLYMORPHISM WITHOUT OVERLOADING AND OVERRRIDING IS POSSIBLE[^],
Interfaces and Polymorphism[^],
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^].

—SA


抽象类:

抽象类提供了一组实现下一个类的规则

规则将通过抽象方法提供

抽象方法不包含任何定义

继承抽象类时,必须覆盖所有抽象方法

如果一个类包含至少一个抽象方法,那么它必须被声明为抽象类

抽象类无法实例化(即我们不能创建对象),但可以创建引用

引用取决于子类对象的内存

抽象类也称为部分抽象类

部分抽象类可能包含带有主体和函数但没有主体的函数

如果一个类包含所有没有主体的函数,那么它被称为完全抽象类(接口)



接口类:



如果一个类包含所有抽象方法,那么该类称为接口

接口支持多重继承

在界面中所有方法默认情况下公共抽象

接口可实现

接口可以实例化,但无法创建参考
Abstract Class:
Abstract class provides a set of rules to implement next class
Rules will be provided through abstract methods
Abstract method does not contain any definition
While inheriting abstract class all abstract methods must be override
If a class contains at least one abstract method then it must be declared as an "Abstract Class"
Abstract classes cannot be instantiated (i.e. we cannot create objects), but a reference can be created
Reference depends on child class object’s memory
Abstract classes are also called as "Partial abstract classes"
Partial abstract class may contain functions with body and functions without body
If a class contains all functions without body then it is called as "Fully Abstract Class" (Interface)

Interface Class:

If a class contains all abstract methods then that class is known as "Interface"
Interfaces support like multiple inheritance
In interface all methods r public abstract by default
Interfaces r implementable
Interfaces can be instantiated, but a reference cannot be created


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

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