简体   繁体   English

Java中的客户端方法是什么?

[英]What are client methods in Java?

I'm relatively new to Java and trying to learn more about the terminology. 我对Java还是比较陌生,因此想进一步了解该术语。 As I was reading, I came across the term client methods in a discussion about public, private and static variables. 在阅读时,在有关公共,私有和静态变量的讨论中遇到了术语“ client methods

Here are a two questions that I have regarding this term: 关于这个词,我有两个问题:

  • Are client methods simply all methods that are not declared within a specific file? client methods是否只是未在特定文件中声明的所有方法? Say I have two files, one called File1.java and the other File2.java . 说我有两个文件,一个叫File1.java ,另一个叫File2.java If File2.java contained a method called printText , would that be considered a client method from the perspective of File1 ? 如果File2.java包含一个名为方法printText ,是否会被认为是client method从的角度File1
  • Why are they called " client " methods, what exactly defines a client? 为什么将它们称为“ client ”方法,确切地定义了客户端? Are there client variables too? 是否还有client variables

Edit: As a reference, I saw this term in the current latest edition of the Barron's AP Computer Science A book , here is a direct quote: 编辑:作为参考,我在Barron的AP计算机科学A书的最新版本中看到了这个术语,这是直接引用:

The variable OVERDRAWN_PENALTY is an example in the BankAccount class. 变量OVERDRAWN_PENALTY是BankAccount类中的一个示例。 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. [...]客户端方法会将变量称为BankAccount.OVERDRAWN_PENALTY。 In its own class it is referred to as simply OVERDRAWN_PENALTY. 在它自己的类中,它简称为OVERDRAWN_PENALTY。

Re-Edit , this part was included a bit before the quote above: 重新编辑 ,这部分包括在上面的引用之前:

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用作“使用该类的方法/不在此类/程序包/库中的client methods ”的缩写。

I think it just means "exposed to a client". 我认为这只是“暴露给客户”的意思。 Think about an API, it can contain any number of methods of various visibility modifiers, but the only ones you can directly use as a "client" are those that are public . 考虑一下API,它可以包含各种可见性修饰符的许多方法,但是唯一可以直接用作“客户端”的方法是public

After reading that a few times, though, it does seem to say a method not within the same class that accesses the current class is considered a "client method". 但是,经过几次阅读后,似乎确实说不在访问当前类的同一个类中的方法被视为“客户端方法”。

The word "client" is typically used to connote "a user of some other system or service." 单词“客户端”通常用于表示“某些其他系统或服务的用户”。

When one speaks of a client process , for example, this is meant to indicate a process that accesses, uses, or consumes services that are provided by some corresponding server program or process . 例如,当谈到客户进程时 ,这是指表示一个访问,使用或使用某些相应服务器程序或进程提供的服务的进程

This language has carried over, more generally, into discussions of APIs. 这种语言更广泛地延续到了API的讨论中。 A "client" of an API is a program (often written by a different programmer) that accesses the services that are defined by the contract of that API. API的“客户端”是访问该API的合同定义的服务的程序(通常由不同的程序员编写)。 As an aside, in Java an API is formed by all the members of the classes and interfaces which can be accessed by classes in different packages (ie the exported members define the API). 顺便说一句,在Java中,由类和接口的所有成员形成API,可以由不同程序包中的类访问(即, 导出的成员定义API)。

In the paragraph provided: 在提供的段落中:

The variable OVERDRAWN_PENALTY is an example in the BankAccount class. 变量OVERDRAWN_PENALTY是BankAccount类中的一个示例。 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. [...]客户端方法会将变量称为BankAccount.OVERDRAWN_PENALTY。 In its own class it is referred to as simply OVERDRAWN_PENALTY. 在它自己的类中,它简称为OVERDRAWN_PENALTY。

The client method refers to a method (probably written by someone else) that accesses the services of the BankAccount class, in this case the public variable in the class. 客户端方法是指一种访问BankAccount类的服务的方法(可能由其他人编写),在这种情况下,该方法是该类中的公共变量。

The term "client" for users of an API (eg a method) is quite common to me. API用户(例如方法)的术语“客户端”对我来说很常见。 So if it's a method that uses another method the first one is a client to the latter. 因此,如果它是使用另一种方法的方法,则第一个是后者的客户端。

It's completely unrelated to "network clients" in this context. 在这种情况下,它与“网络客户端”完全无关。

One – rather authoritative – source for this is the book "Effective Java" by Joshua Bloch. 约书亚·布洛赫(Joshua Bloch)撰写的“有效Java”一书是该书的权威来源。

Let me quote two examples: 让我举两个例子:

The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor. 类允许客户端获取其自身实例的通常方法是提供一个公共构造函数。

and: 和:

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. 简而言之,伸缩构造函数模式有效,但是当有许多参数时,很难编写客户端代码,并且仍然很难读取。

You may easily come up with "client method" then I think. 我想,您可能会轻松提出“客户端方法”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM