Java 什么是类的客户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30512953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Exactly is a Client of a Class
提问by Insignificant Person
I always thought a client of a class is one that uses a reference to access instance fields/methods of that class i.e. another class (from its own methods). But when I think about some details there are some things that I couldn't figure out.
我一直认为一个类的客户端是一个使用引用来访问该类的实例字段/方法的客户端,即另一个类(从它自己的方法)。但是当我想到一些细节时,有些事情我想不通。
In Java I know of a few places where you can place action statements.
在 Java 中,我知道有几个地方可以放置动作语句。
- Inside a method (instance/static)
- Inline initialization (when you declare an instance variable like in private int x = 5;)
- Static block
- 方法内部(实例/静态)
- 内联初始化(当你像在 private int x = 5; 中声明一个实例变量时)
- 静态块
Maybe there are more that I don't know or remember.
也许还有更多我不知道或不记得的。
The first part of the question isare all of these considered clients if they are in another class? Also can they access private stuff when they are in the same class?
问题的第一部分是,如果他们在另一个班级,所有这些被视为客户吗?当他们在同一个班级时,他们也可以访问私人物品吗?
The second part is:When JVM calls a method (like main and finalize) are they considered clients, too? (the calls are not from a class?)
第二部分是:当 JVM 调用一个方法(如 main 和 finalize)时,它们是否也被视为客户端?(这些电话不是来自一个班级?)
Edit:Can a client access only public methods and variables? Can't we call it a client if it accesses package fields (if in the same package).
编辑:客户端只能访问公共方法和变量吗?如果它访问包字段(如果在同一个包中),我们不能称它为客户端。
回答by Prahalad Deshpande
Before diving into Java -lets try to map the situation to a physical world scenario.
在深入研究 Java 之前,让我们尝试将情况映射到物理世界场景。
You go to a pastry shop and order a pastry. You get the pastry from the store pay for it and leave. You naturallycall yourself a clientof the store. So what did we understand from this?
您去糕点店订购糕点。你从商店买糕点付钱然后离开。你很自然地称自己为商店的客户。那么我们从中了解到了什么?
The term clientrefers to any entity that requests a service from another entity. The client does not bother about how the entity providing the service actually provides the service - the client is happy as long as the service is available and fulfills its use case.
术语客户端是指从另一个实体请求服务的任何实体。客户端不会关心提供服务的实体如何实际提供服务 - 只要服务可用并满足其用例,客户端就会很高兴。
Hence, when a method M1within a class calls another method M2then M1is a client of M2. Similarly when a class C1requests service of C2then C1 is a client of C2.
因此,当类中的方法M1调用另一个方法M2 时,则M1是M2的客户端。类似地,当类C1请求C2 的服务时,C1 是 C2 的客户端。
Coming to your question about main(), finalize()and the interaction with JVM - You can consider the class-loader of the JVM as the client of your class since it loads JVM class loader will load your class and then requestthe main method to begin execution and continue further processing.
EDIT based on comment from the OP- finalize()
method is accessed by the Garbage Collector within the JVM using some internal JVM tricks. As a general rule within any normal application will not perform such trickery.
关于main()、finalize()以及与 JVM 的交互的问题 - 您可以将 JVM 的类加载器视为类的客户端,因为它加载 JVM 类加载器将加载您的类,然后请求main 方法开始执行并继续进一步处理。
基于来自 OP 的评论进行编辑- finalize()
方法由 JVM 中的垃圾收集器使用一些内部 JVM 技巧访问。作为任何正常应用程序中的一般规则,不会执行此类欺骗。
回答by iullianr
A client of a class, in the generally accepted use of the term, is any other class that uses reference access methods (less likely fields due to the principle of encapsulation in OOP). What you have enumerated are not considered clients in any way. But the term is part of a common programming language, it is not like there is a big theory behind it. It is just what majority of the programmers understand by it.
类的客户端,在该术语的普遍接受的用法中,是使用引用访问方法的任何其他类(由于 OOP 中的封装原则,不太可能使用字段)。您所列举的内容在任何情况下都不会被视为客户。但该术语是通用编程语言的一部分,它背后并没有什么大理论。这正是大多数程序员所理解的。
回答by Sergei Petunin
I don't believe there exists a strict definition of "client of class". I usually say "client code", that is, one piece of code that uses another piece of code through some sort of contract. From this point of view, even JVM, when calling your class' finalize
method, can be considered client code for the code of your class, as it works with your class through a specific interface defined in Object
class (its finalize
method).
我不相信“阶级的客户”存在严格的定义。我通常说的“客户端代码”,即一段代码通过某种契约使用另一段代码。从这个角度来看,即使 JVM 在调用您的类的finalize
方法时,也可以被视为您的类代码的客户端代码,因为它通过Object
类中定义的特定接口(其finalize
方法)与您的类一起工作。
Anyway, the point here is not how you call it, but what you make of it. Is calling JVM the client of your class means something to you, or affects the way you write your code? I think not. So I prefer to talk about the notion of contract (interface) between two pieces of code.
无论如何,这里的重点不是你如何称呼它,而是你如何理解它。将 JVM 称为您的类的客户端对您来说意味着什么,还是会影响您编写代码的方式?我想不是。所以我更喜欢谈论两段代码之间的契约(接口)的概念。
回答by user1133275
... a client of a class is one that uses a reference to access ... fields/methods of that class
...一个类的客户端是一个使用引用来访问该类的字段/方法的客户端
Correct but not limited to instances
正确但不限于实例
... are all of these considered clients if they are in another class?
...如果他们在另一个班级,所有这些都被视为客户吗?
Yes methods and variables are clients of some other class if they reference it.
是的,如果方法和变量引用它,它们就是其他类的客户。
Note that inline initialization is actually a short hand for initialization in the constructor method, and static blocks for the static initializer.
请注意,内联初始化实际上是构造函数方法中初始化的简写,以及静态初始化程序的静态块。
can [clients] access private stuff [from some other class]?
[客户]可以[从其他班级]访问私人资料吗?
Only if the other class permits it (like returning a private object from a getter method). Same for protected stuff unless it's in the same package, the JVM itself is not subject to private/protected limitations.
仅当其他类允许时(例如从 getter 方法返回私有对象)。受保护的东西也一样,除非它在同一个包中,JVM 本身不受私有/受保护的限制。
When JVM calls a method (like main and finalize) are they considered clients, too? (the calls are not from a class?)
当 JVM 调用一个方法(如 main 和 finalize)时,它们是否也被视为客户端?(这些电话不是来自一个班级?)
The JVM or one of it's components is considered the client in that case. The calls may be from a class in the JVM depending on the JVM implementation.
在这种情况下,JVM 或其组件之一被视为客户端。调用可能来自 JVM 中的类,具体取决于 JVM 实现。
回答by sviklim
I'm not sure, that there is a strict definition of 'client' in Java. Actually, I'd rather suggest this term only regarding interfaces, since they enforce encapsulation and avoid static calls. Since there are different accessibility modes, which are applied to methods and fields, then it's hard to predict and define which exact call is 'client' and which is not. Moreover, another question rises: 'Should we call child class a "client" one since it may use parent's methods?'
我不确定 Java 中是否有严格的“客户端”定义。实际上,我宁愿只建议关于接口的术语,因为它们强制执行封装并避免静态调用。由于有不同的可访问性模式应用于方法和字段,因此很难预测和定义哪个确切的调用是“客户端”,哪个不是。此外,另一个问题出现了:“我们是否应该将子类称为“客户端”类,因为它可能使用父类的方法?