Java 中的客户端方法是什么?
What are client methods in Java?
我对 Java 比较陌生,正在尝试了解更多有关术语的信息。在阅读时,我在关于 public、私有变量和静态变量的讨论中遇到了术语 client methods
。
这里有两个关于这个词的问题:
client methods
只是所有未在特定文件中声明的方法吗?假设我有两个文件,一个名为 File1.java
,另一个名为 File2.java
。如果 File2.java
包含一个名为 printText
的方法,那么从 File1
的角度来看,它会被认为是 client method
吗?
- 为什么它们被称为“
client
”方法,到底什么定义了客户端?还有client variables
吗?
编辑:作为参考,我在Barron's AP Computer Science A book当前最新版看到这个词,这里直接引用:
The variable OVERDRAWN_PENALTY is an example in the BankAccount class.
Since the variable is public, it can be used in any client method. [...] A
client method would refer to the variable as
BankAccount.OVERDRAWN_PENALTY. In its own class it is referred to as
simply OVERDRAWN_PENALTY.
重新编辑,这部分包含在上面引用之前的一点:
Similarly public methods are accessible to all client programs.
Clients, however, are not privy to the class implementation and may
not access the private instance variables and private methods of the
class.
好的,根据您的评论,本书的这一部分似乎使用术语 client methods
作为 "methods not in this class/package/library that use this class" 的缩写版本。
我认为它只是意味着"exposed to a client"。想想一个 API,它可以包含任意数量的各种可见性修饰符的方法,但是唯一可以直接用作 "client" 的方法是 public
。
不过,在阅读了几次之后,它似乎确实说不在同一个 class 中的方法访问当前 class 被认为是 "client method"。
单词 "client" 通常用于表示 "a user of some other system or service."
例如,当谈到 客户端进程 时,这意味着一个进程访问、使用或消费由某些相应的 [=22] 提供的服务=]服务器程序或进程.
这种语言更普遍地延续到 API 的讨论中。 API 的 "client" 是一个程序(通常由不同的程序员编写),它访问由 API 的合同定义的服务。顺便说一句,在 Java 中,API 由 classes 的所有成员和接口组成,可以被不同包中的 classes 访问(即 导出成员定义API).
在提供的段落中:
The variable OVERDRAWN_PENALTY is an example in the BankAccount class.
Since the variable is public, it can be used in any client method.
[...] A client method would refer to the variable as
BankAccount.OVERDRAWN_PENALTY. In its own class it is referred to as
simply OVERDRAWN_PENALTY.
客户端方法指的是访问BankAccount
class服务的方法(可能是别人写的),这里是public 变量在 class.
术语 "client" 用于 API(例如方法)的用户对我来说很常见。因此,如果它是一种使用另一种方法的方法,则第一种方法是后者的客户端。
在此上下文中,它与 "network clients" 完全无关。
一个——相当权威的——来源是约书亚布洛赫的书"Effective Java"。
我举两个例子:
The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor.
和:
In short, the telescoping constructor pattern works, but it is hard to write
client code when there are many parameters, and harder still to read it.
我想你可能很容易想出"client method"。
我对 Java 比较陌生,正在尝试了解更多有关术语的信息。在阅读时,我在关于 public、私有变量和静态变量的讨论中遇到了术语 client methods
。
这里有两个关于这个词的问题:
client methods
只是所有未在特定文件中声明的方法吗?假设我有两个文件,一个名为File1.java
,另一个名为File2.java
。如果File2.java
包含一个名为printText
的方法,那么从File1
的角度来看,它会被认为是client method
吗?- 为什么它们被称为“
client
”方法,到底什么定义了客户端?还有client variables
吗?
编辑:作为参考,我在Barron's AP Computer Science A book当前最新版看到这个词,这里直接引用:
The variable OVERDRAWN_PENALTY is an example in the BankAccount class. Since the variable is public, it can be used in any client method. [...] A client method would refer to the variable as BankAccount.OVERDRAWN_PENALTY. In its own class it is referred to as simply OVERDRAWN_PENALTY.
重新编辑,这部分包含在上面引用之前的一点:
Similarly public methods are accessible to all client programs. Clients, however, are not privy to the class implementation and may not access the private instance variables and private methods of the class.
好的,根据您的评论,本书的这一部分似乎使用术语 client methods
作为 "methods not in this class/package/library that use this class" 的缩写版本。
我认为它只是意味着"exposed to a client"。想想一个 API,它可以包含任意数量的各种可见性修饰符的方法,但是唯一可以直接用作 "client" 的方法是 public
。
不过,在阅读了几次之后,它似乎确实说不在同一个 class 中的方法访问当前 class 被认为是 "client method"。
单词 "client" 通常用于表示 "a user of some other system or service."
例如,当谈到 客户端进程 时,这意味着一个进程访问、使用或消费由某些相应的 [=22] 提供的服务=]服务器程序或进程.
这种语言更普遍地延续到 API 的讨论中。 API 的 "client" 是一个程序(通常由不同的程序员编写),它访问由 API 的合同定义的服务。顺便说一句,在 Java 中,API 由 classes 的所有成员和接口组成,可以被不同包中的 classes 访问(即 导出成员定义API).
在提供的段落中:
The variable OVERDRAWN_PENALTY is an example in the BankAccount class. Since the variable is public, it can be used in any client method. [...] A client method would refer to the variable as BankAccount.OVERDRAWN_PENALTY. In its own class it is referred to as simply OVERDRAWN_PENALTY.
客户端方法指的是访问BankAccount
class服务的方法(可能是别人写的),这里是public 变量在 class.
术语 "client" 用于 API(例如方法)的用户对我来说很常见。因此,如果它是一种使用另一种方法的方法,则第一种方法是后者的客户端。
在此上下文中,它与 "network clients" 完全无关。
一个——相当权威的——来源是约书亚布洛赫的书"Effective Java"。
我举两个例子:
The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor.
和:
In short, the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
我想你可能很容易想出"client method"。