抽象和接口类

huangapple go评论74阅读模式
英文:

Abstract and Interface class

问题

以下是翻译好的内容:

下面的陈述正确吗?
如果 A 和 B 都是接口或类,则 A 继承 B 是正确的。

这怎么可能是真的呢?如果 A 是一个接口,而 B 是一个类,那么接口是不能继承类的。或者说,我们可以吗?如果可以的话,请提供一些支持性的示例。

英文:

Is the below statement correct ?
A extends B is correct if both A and B are either interfaces or classes

How can it be true ? If A is an Interface and B is a class then Interface cannot inherit a class. Or can we ? If so, can you give some supporting examples please.

答案1

得分: 1

根据上下文,'extends' 用于表示“类型 X 是类型 Y 的子类型”的一般概念。

例如,给定以下代码:

interface INTERFACE {}

class CLASS implements INTERFACE {}

那么可以肯定地说 CLASS implements INTERFACE

根据上下文,有时可以说 CLASS extends INTERFACE

例如,INTERFACE x = new CLASS(); 是合法的,这表明在这个情况下,CLASSINTERFACE 的子类型。

作为第二个例子,在泛型约束中,关键字 extends 用于指示子类型关系,这可以通过接口来实现。以下是真实的 Java 代码示例:

interface INTERFACE {}

class CLASS implements INTERFACE {}

List<? extends INTERFACE> someList;
someList = new ArrayList<CLASS>();

注意,这里使用了 extends。如果你写成了 List<? implements INTERFACE>,那将会是编译时错误。

如果这是某种作业问题,术语的问题在于没有“审判者德莱德”(注:这里暗指没有绝对权威的标准)。没有人手里拿着术语书,会因为你用词不当而对你狠狠地批评。如果有人在随意的交谈中决定说,比如,ArrayList extends List(注意ArrayList是一个类,List是一个接口),讨论“对”和“错”就没有太多意义了。张嘴让空气从你的声带中通过的目的是为了交流,传达概念和思想。

如果张嘴的目的是要传达一个观点,即 ArrayListList 的子类型,并且在代码需要 List 的任何地方都可以满足需求,那么我个人认为用“ArrayList extends List”来缩短表达是可以的…… 而且,在传达 ArrayList 是子类型的观点方面,这是完全正确的。

如果你决定听到“如果我写 class ArrayList extends List {},那么它会编译通过”——好吧,那你就面临一个经典的沟通问题。说话者意图一件事,你理解成了另一回事。这种情况经常发生。在选择用词时更加小心,并更加接近语言规范,这可以帮助,但正如我已经展示的,Java 本身在某些上下文中使用 extends 来表示“子类型”,而在其他上下文中,它仅表示“这是我的父类”,这些概念并不完全相同。

英文:

Depending on context, 'extends' is used for the general notion of 'type X is a subtype of type Y'.

For example, given:

interface INTERFACE {}

class CLASS implements INTERFACE {}

Then it is certainly correct to say that CLASS implements INTERFACE.

Depending on context, one might say that CLASS extends INTERFACE.

For example, INTERFACE x = new CLASS(); would be legal, which shows that in the end, CLASS is a subtype of INTERFACE here.

As a second example, in generics bounds, the keyword extends is used to indicate subtype relationships, which may be fulfilled with interfaces. For example, this is real java code:

interface INTERFACE {}

class CLASS implements INTERFACE {}

List&lt;? extends INTERFACE&gt; someList;
someList = new ArrayList&lt;CLASS&gt;();

Note, extends is used here. Had you written List&lt;? implements INTERFACE&gt;, that'd be a compile time error.

If this is some sort of homework question, the problem with terminology is that there is no Judge Dredd.

There's nobody out there with a book of terms in hand who will slap you into next week if you get it wrong. If someone decides to say in casual conversation that, say, ArrayList extends List (note that ArrayList is a class, List is an interface), it's not particularly useful to talk about right and wrong. The point of opening your mouth and making air pass through your vocal cords is to communicate; to convey notions and ideas.

If the point of the exercise of opening your yap was to convey the notion that ArrayList is a subtype of List and will suffice for any and all places where code needs a List, well, I'd personally say trying to shorten that by saying 'ArrayList extends List' is fair game... - and, insofar as the point is to convey that ArrayList is a subtype, it is entirely correct.

If you decide to hear 'if I write class ArrayList extends List {}, then it'll compile' - okay, then you have a classic issue of bad communication. The talker intended one thing, you understood something else. Happens all the time. Being more careful with word choices and steering closer to the language spec can help, but as I showed already, java itself uses extends in certain contexts to mean 'subtype of', and in other contexts, specifically it means only 'this is my parent class', and these concepts are not quite the same.

答案2

得分: 1

以下是翻译好的部分:

在这种情况下,我猜可能的输出如下:

  1. A扩展B(如果两者都是接口)
  2. A扩展B(如果两者都是类)

在情况1中,一个接口可以毫无问题地扩展另一个接口。

英文:

In this case these are the possible outputs I guess -

  1. A extends B (if both are interfaces)
  2. A extends B (If both are classes)

In case of no 1, An interface can extend another interface without any problem.

The rest is answered already by @rzwitserloot.

答案3

得分: 0

一个接口可以扩展其他接口。

一个类可以实现多于1个接口。

一个类只能扩展1个类。

一个接口不能扩展类。

我们在Java中使用接口的概念,因为如果我们想要在Java中使用多重继承。直到Java7,我们不能在接口中编写非抽象方法,但是从Java8开始,我们可以使用"default"和"static"关键字来编写非抽象方法。

英文:

An interface can extend other interfaces.

A class implements more than 1 interface.

A class extends only 1 class.

An interface can't extend a class

we use the interface concept in java because if we want to use multiple inheritances in java. Until Java7 we can't write a non-abstract method in the interface but from java8 we can write a non-abstract method using default and static keyword.

huangapple
  • 本文由 发表于 2020年9月15日 10:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定