有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的“原始接口方法”是什么?

我正在读《有效的Java》,下面是这本书的一句话:

The interface defines the type, perhaps providing some default methods, while the skeletal implementation class implements the remaining non-primitive interface methods atop the primitive interface methods. Extending a skeletal implementation takes most of the work out of implementing an interface. This is the Template Method pattern.

作者正在讨论一些抽象类,如AbstractCollectionAbstractSet等,它们实现了一个接口并提供了一些基本的实现。然而,我不知道引用中提到的non-primitive interface methodsprimitive interface methods是什么。我知道Java中的“基本类型”,但什么是“基本方法”


共 (1) 个答案

  1. # 1 楼答案

    在这种情况下,“基元方法”与基元类型没有任何关系,“基元”的含义在这里是不同的。根据"Method Properties in Java" (p. 3),一个基本方法执行一项基本任务,它不依赖任何其他方法来帮助完成它的工作

    A primitive method is a method that carries out one specific task, usually by directly referring to the fields of the object. It does not rely on any (non-primitive) methods of the class that defines the primitive method.

    这与依赖于调用其他方法来执行子任务的“组合方法”相反。原语方法似乎执行的任务不是或不应该被分解成由其他方法表示的更小的子任务

    例如,您可能有一个Time类,它有小时和分钟。基本方法可以是每个小时和分钟字段的单独设置器,例如setHoursetMinutes。组合方法,例如setTime,可以调用setHoursetMinutes来完成其工作

    模板方法模式涉及创建一个组合方法,该方法定义要完成的任务的工作流的顺序和结构,并调用其他可能是基本的方法。随着Java8中default方法的出现,这些方法有可能出现在接口中