gpt4 book ai didi

java - Java 中的 "primitive interface method"是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:29:20 26 4
gpt4 key购买 nike

我正在读《Effective 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等抽象类,实现了一个接口(interface),提供了一些基本的实现。但是,我不知道引用中提到的non-primitive interface methodsprimitive interface methods是什么。我知道 Java 中的“原始类型”,但什么是“原始方法”?

最佳答案

在此上下文中,“原始方法”与原始类型没有任何关系——“原始”的含义在这里有所不同。根据"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 类。原始方法可以是每个小时和分钟字段的单独 setter ,例如setHoursetMinutes。组合方法,例如setTime,可以调用 setHoursetMinutes 来完成它的工作。

模板方法模式涉及创建一个组合方法,该方法定义要完成的任务的工作流的顺序和结构,调用其他可能是原始的方法。随着 Java 8 中 default 方法的出现,这些方法可能位于接口(interface)中。

关于java - Java 中的 "primitive interface method"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com