默认接口方法.现在,抽象类和接口之间有什么深刻的区别? [英] Default Interface Methods. What is deep meaningful difference now, between abstract class and interface?

查看:32
本文介绍了默认接口方法.现在,抽象类和接口之间有什么深刻的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道抽象类是一种无法实例化的特殊类.抽象类只能被子类化(继承自).换句话说,它只允许其他类继承它,但不能实例化它.优点是它可以为所有子类强制执行某些层次结构.简单来说,它是一种契约,强制所有子类执行相同的层次结构或标准.

I know that an abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but, it cannot be instantiated. The advantage is that it can enforce certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

我也知道接口不是类.它是由接口"一词定义的实体.接口没有实现;它只有签名,或者换句话说,只有没有主体的方法的定义.作为与 Abstract 类的相似之处之一,它是一个契约,用于定义所有子类的层次结构,或者定义特定的一组方法及其参数.它们之间的主要区别在于一个类可以实现多个接口,但只能从一个抽象类继承.由于C#不支持多重继承,所以使用接口来实现多重继承.

Also I know that An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

当我们创建一个接口时,我们基本上是在创建一组没有任何实现的方法,这些实现必须被实现的类覆盖.优点是它提供了一种方法让一个类成为两个类的一部分:一个来自继承层次结构,一个来自接口.

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

当我们创建一个抽象类时,我们正在创建一个基类,它可能有一个或多个已完成的方法,但至少有一个或多个方法未完成并声明为抽象.如果抽象类的所有方法都未完成,则它与接口相同.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface.

但是但是但是

我注意到我们将有C# 8.0 中的默认接口方法

也许我问这个是因为我只有 1-2 年的编程经验,但是 现在抽象类和接口之间的主要区别是什么?

Maybe I'm asking it because I have only 1-2 years of experience in programming, but what would be main difference between abstract class and interface now?

我知道我们不能在界面中创建状态,它们之间只有一个区别吗?

I know that we can't make state in interface, will it be only one difference between them?

推荐答案

除了抽象类可以有状态而接口不能有这一显而易见的事实之外,两者之间没有很多区别.默认方法或也称为虚拟扩展方法实际上已经在 J​​ava 中可用了一段时间.默认方法的主要驱动力是接口演变,这意味着能够在未来版本中向接口添加方法,而不会破坏该接口现有实现的源代码或二进制兼容性.

There is not a lot of difference between the two apart from the obvious fact that abstract classes can have state and interfaces cannot. Default methods or also known as virtual extension methods have actually been available in Java for a while. The main drive for default methods is interface evolution which means being able to add methods to an interface in future versions without breaking source or binary compatibility with existing implementations of that interface.

帖子提到的另外几个优点:

another couple of good points mentioned by this post:

  • The feature enables C# to interoperate with APIs targeting Android (Java) and iOs (Swift), which support similar features.
  • As it turns out, adding default interface implementations provides the elements of the "traits" language feature (https://en.wikipedia.org/wiki/Trait_(computer_programming)). Traits have proven to be a powerful programming technique (http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf).

这篇关于默认接口方法.现在,抽象类和接口之间有什么深刻的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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