默认接口方法。现在,抽象类和接口之间有什么深远的有意义的区别?

编程入门 行业动态 更新时间:2024-10-19 09:34:44
本文介绍了默认接口方法。现在,抽象类和接口之间有什么深远的有意义的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道抽象类是无法实例化的一种特殊类。抽象类仅是子类(从继承)。换句话说,它仅允许其他类从中继承而不能实例化。优点是它为所有子类强制执行某些层次结构。简单来说,它是一种契约,它迫使所有子类都遵循相同的层次结构或标准。

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 cannot be instantiated. The advantage is that it enforces 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.

BUT BUT 但是

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

I noticed that we will have Default Interface Methods in 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?

推荐答案

除了明显的事实,即抽象类可以具有状态而接口则不能,这两者之间没有很大的区别。默认方法或称为虚拟扩展方法的Java实际上已经有一段时间了。默认方法的主要驱动力是接口演化,这意味着可以在将来的版本中向接口添加方法,而不会破坏与该接口的现有实现的源代码或二进制兼容性。

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:

  • 该功能使C#可以与支持类似功能的Android (Java)和iOs(Swift)的API互操作。 li>
  • 事实证明,添加默认接口实现可为提供特征语言功能的元素( en.wikipedia/wiki/Trait_(computer_programming))。特质已被证明是一种强大的编程技术( http:/ /scg.unibe.ch/archive/papers/Scha03aTraits.pdf )。
  • 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 (en.wikipedia/wiki/Trait_(computer_programming)). Traits have proven to be a powerful programming technique (scg.unibe.ch/archive/papers/Scha03aTraits.pdf).

更多推荐

默认接口方法。现在,抽象类和接口之间有什么深远的有意义的区别?

本文发布于:2023-11-22 14:50:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   有什么   有意义   深远   区别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!