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

查看:115
本文介绍了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.

推荐答案

根据 JLS 8.4.3.1


抽象方法声明将方法引入为成员,并提供其签名(第8.4.2节),结果( §8.4.5),如果有则抛出throw子句(§8.4.6),但不提供实现(§8.4.7)。不是抽象可以称为 concrete 方法。

"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."

根据< a href = https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-9.4 rel = noreferrer> JLS 9.4 :


默认方法是在接口中使用 default 修饰符声明的实例方法。主体始终由一个块表示,该块提供了默认实现任何实现该接口而不覆盖该方法的类。 默认方法不同于在类中声明的具体方法(第8.4.3.1节),也不同于私有接口方法,后者既不继承也不被覆盖。 p>

"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:


  • 抽象方法,

  • 具体方法,

  • 默认方法和

  • 私有接口方法

  • abstract methods,
  • concrete methods,
  • default methods and
  • private interface methods

请注意,JLS 8.4.3.1并未提及 final static 区分抽象方法和具体方法。

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

这些修饰符不能与抽象关键字一起使用,这意味着静态 final 必须是具体方法。此加强 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天全站免登陆