JLS 如何指定术语“抽象方法"、“具体方法"?和“默认方法"? [英] How does the JLS specify the terms "abstract method", "concrete method" and "default method"?

查看:27
本文介绍了JLS 如何指定术语“抽象方法"、“具体方法"?和“默认方法"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些 StackOverflow 答案中看到了术语抽象方法具体方法默认方法的不同"定义.

I have seen "divergent" definitions of the terms abstract method, concrete method and default method in some StackOverflow answers.

Java 语言规范给出的真正定义是什么?请在您的答案中包含相关的支持 JLS 参考.

What are the real definitions, as given by the Java Language Specification? Please include the relevant supporting JLS references in your answer.

推荐答案

下面的链接/部分编号取自 Java 11 版本的 Java 语言规范.

The links / section numbers below are taken from the Java 11 version of the Java Language Specification.

根据 JLS8.4.3.1:

"abstract 方法声明将方法作为成员引入,提供其签名(第 8.4.2 节)、结果(第 8.4.5 节)和 throws 子句(如果有)(第 8.4.2 节).6),但不提供实现(第 8.4.7 节).抽象的方法可以称为具体方法."

"An abstract method declaration introduces the method as a member, providing its signature (§8.4.2), result (§8.4.5), and throws clause if any (§8.4.6), but does not provide an implementation (§8.4.7). A method that is not abstract may be referred to as a concrete method."

根据 JLS 9.4:

"默认方法是在带有 default 修饰符的接口中声明的实例方法.它的主体总是由一个块表示,它为任何实现接口的类提供默认实现,而不覆盖方法.默认方法不同于在类中声明的具体方法(第 8.4.3.1 节),也不同于既不继承也不重写的私有接口方法."

"A default method is an instance method declared in an interface with the default modifier. Its body is always represented by a block, which provides a default implementation for any class that implements the interface without overriding the method. Default methods are distinct from concrete methods (§8.4.3.1), which are declared in classes, and from private interface methods, which are neither inherited nor overridden."

所以根据这个分类法,实际上有 4 种方法:

So according to this taxonomy there are really 4 types of methods:

  • 抽象方法,
  • 具体方法,
  • 默认方法和
  • 私有接口方法

请注意,JLS 8.4.3.1 在抽象方法和具体方法的区别中没有提到 finalstatic.

Note that JLS 8.4.3.1 makes no mention of final or static in the distinction between abstract and concrete methods.

这些修饰符不能与 abstract 关键字一起使用.这意味着 staticfinal 方法必须是具体方法.这加强了具体方法的 8.4.3.1 定义.

These modifiers cannot be used with the abstract keyword. This implies that methods that are static or final must be concrete methods. This reinforces the 8.4.3.1 definition of a concrete method.

这篇关于JLS 如何指定术语“抽象方法"、“具体方法"?和“默认方法"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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