简体   繁体   English

为什么一个类的实例可以访问它自己类型的另一个实例的私有字段?

[英]Why can an instance of a class access private fields of another instance of its own type?

An instance of a class, in Java, can access private fields of a different instance of its own type, such as in the following listing:在 Java 中,类的实例可以访问其自身类型的不同实例的私有字段,如下面的清单所示:

public class Foo {
  private int secret;
  public void bar(final Foo foo) {
    foo.secret = 100;
  }
}

What would be the argument for such semantics (when designing a language)?这种语义(设计语言时)的论据是什么?

First you have to ask "why have private fields at all?"首先你要问“为什么有私有字段?”

Private fields are primarily for encapsulation: a consumer of a class shouldn't have to know the internals of that class' implementation, and in fact those internals should be actively hidden from the consumer.私有字段主要用于封装:类的消费者不应该知道该类实现的内部结构,实际上这些内部结构应该对消费者主动隐藏。 Otherwise, if a user relied on those internals, then the implementer would be forced to support them or break backwards compatibility.否则,如果用户依赖这些内部结构,那么实现者将被迫支持它们或破坏向后兼容性。 In other words, it protects both the user and designer of the class:换句话说,它保护了类的用户和设计者:

  • users are protected from implementation changes breaking their code保护用户免受破坏其代码的实现更改
  • the designer is protected from having to keep implementation details features unchanged forever保护设计者不必永远保持实现细节特性不变

But a class doesn't need to be protected from itself;但是一个类不需要被自身保护; it doesn't need to worry about the case where one bit of its code changes, but another bit (that uses the first bit) can't change.它不需要担心它的代码的一位更改,但另一位(使用第一位)无法更改的情况。 Backwards compatibility is not a concern, because the class is developed and deployed as a single, atomic chunk of code.向后兼容性不是问题,因为该类是作为单个原子代码块开发和部署的。 In other words, neither of the above protections are needed.换句话说,上述保护都不需要。

Since there's no need to protect the fields, and since it's often necessary to see them (for instance, to compare if two objects are equal), they're visible within the class.由于不需要保护字段,而且通常需要查看它们(例如,比较两个对象是否相等),因此它们在类中是可见的。

The private field is meant to tell other programmers not to mess with it. private字段是为了告诉其他程序员不要乱用它。

Presumably, everyone working in a single class knows what all the variables do.据推测,在一个班级中工作的每个人都知道所有变量的作用。 The private field doesn't hide your own code from you, just from outside. private字段不会向您隐藏您自己的代码,只是向外部隐藏。

You can copy/compare values without additional getters and change field without setters.您可以在没有额外 getter 的情况下复制/比较值,也可以在没有 setter 的情况下更改字段。 Don't know if in the JVM such simple methods invocations are optimized in any way, but if not, then it produces some overhead.不知道在 JVM 中是否以任何方式优化了这种简单的方法调用,但如果没有,那么它会产生一些开销。

One may think that keeping such "open" access may lead to some security issues, but when implemeting a class you provide all methods for manipulation of these variables.人们可能认为保持这种“开放”访问可能会导致一些安全问题,但是在实现一个类时,您提供了用于操作这些变量的所有方法。 No one can change them from classes extending this class.没有人可以从扩展这个类的类中改变它们。 As a matter of fact, they are still private - accessible only from your code.事实上,它们仍然是私有的——只能从您的代码中访问。

Keep also in mind that logically a class often is destined to do one job.还要记住,从逻辑上讲,一个班级通常注定要完成一项工作。 It can be beneficial to share some information and ease access, especially in cases when large amounts of instances are produced.共享一些信息并简化访问可能是有益的,尤其是在生成大量实例的情况下。 When dealing with cases when more control is needed one can always use package access modifier (which is more "private" in a sense...) or limit instance count with a singleton/factory pattern.在处理需要更多控制的情况时,可以始终使用包访问修饰符(在某种意义上更“私有”......)或使用单例/工厂模式限制实例计数。

暂无
暂无

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

相关问题 如何在另一个实例的自身方法中使用类的实例? - How to use instance of a class in its own method for another instance? 无法通过子类实例从自己的类访问私有变量 - Can't access private variable from own class via subclass instance 如果线程只能访问具有不同线程创建的具有自己实例的类的静态方法,则该线程将在哪个线程上执行? - If threads only can access static methods to a class with its own instance, created by a different thread, what thread will this execute on? Java:直接从同一个类的另一个实例访问私有字段 - Java: Accessing private fields directly from another instance of the same class 类方法如何访问同一类另一个实例的私有成员? - How can a class method access a private member of another instance of the same class? 为什么不同包中的子类无法通过超类实例访问其在另一个包中的超类的受保护字段? - Why a subclass in a different package is unable to access the protected fields of its superclass in another package through superclass instance? 如何使toString()方法返回Super Class私有字段及其实例字段? - How make toString() method return Super Class private fields also along with its instance fields? 如果实例的私有变量在同一类中,您可以访问它吗? (备份) - Can you access a private variable of an instance if it is in the same class? (copyOf) 为什么您可以访问和操纵班级的私人成员 - Why can you access and manipulate private Members of your own class 如何创建具有类型参数的泛型类的实例,该类在其自己的类中扩展了静态类? - How to create an instance of a generic class that has a type paramater which extends a static class within its own class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM