简体   繁体   English

它是不可变的类吗?

[英]Is It Immutable Class?

Does this below class is immutable? 这下课是一成不变的吗?

class Immutable {

  private int x;

  Immutable(int value) {
    this.x = value;
  }

  public int getX(){
    return x;
  }
}
  1. Class is not final Since no member is visible to sub class 类不是最终的,因为子类看不到任何成员

  2. Instance Variable x is not final since there is no setter method. 实例变量x不是最终变量,因为没有设置方法。

Is there any possiblity this class will break the contract of Immutable functionality without adding any code in this class? 此类在不添加任何代码的情况下是否有可能打破不可变功能的约定?

The class is practically immutable. 该类实际上是不可变的。

But you can still use reflection to set the value of the variable x since the variable is not final. 但是您仍然可以使用反射来设置变量x的值,因为该变量不是最终变量。

The Immutable class is immutable. Immutable类是不可变的。

However, it is possible to declare a subclass of Immutable whose instances are mutable. 但是,可以声明其实例是可变的Immutable的子类。 Indeed, the only way to prevent this is to make this class final ... or make its constructor private. 确实,防止这种情况的唯一方法是使此类成为final ...或将其构造函数设为私有。

You could argue (and I would) that a mutable subclass of Immutable is violating the contract of Immutable . 您可能会((我会)认为)可变的Immutable子类违反了Immutable的约定。


The approach of changing the value of x using reflection is breaking the rules. 使用反射更改x值的方法违反了规则。 The Java specs say that the behavior of an application that does this is unspecified. Java规范说未指定执行此操作的应用程序的行为。 (Certainly, it breaks all sorts of assumptions that the JLS says that the JIT compiler is allowed to make.) (当然,它打破了JLS说允许JIT编译器进行的各种假设。)

A sensible developer won't do that kind of thing, will discount the possibility of someone else doing it, and will say that Immutable is immutable despite the theoretical possibility that some total lunatic could change it. 一个明智的开发人员不会做这种事情,会轻视别人做某事的可能性,并且会说尽管某些疯子可能会改变它,但Immutable是不可变的。

Joshua Bloch in his "Effective Java" states that "you should either design your classes for extensibility or make them non-extensible" . 约书亚·布洛赫(Joshua Bloch)在他的“有效Java”中指出: “您应该为可扩展性设计类,或者使它们不可扩展”

If flexibility is your primary concern and you have a strong reason to design your immutable class for inheritance - leave this class non-final. 如果灵活性是您的首要考虑因素,并且您有充分的理由设计不可变的类进行继承-请将该类保留为非最终类。

If security is your concern - make this class final and member field final. 如果您担心安全性-将该课程定为最终课程,将成员字段定为最终课程。 Because: 因为:

  • Non-final classes can be extended to create a mutable child classes by overriding getter method. 通过覆盖getter方法,可以扩展非最终类以创建可变的子类。

  • Java is reflective by design. Java在设计上是反射性的。 As Belgther told it's possible to set the value of member field using reflection. 正如Belgther所说,可以使用反射来设置member field的值。 (no matter private or public) A long time ago I used this to integrate my specific debugger with another project. (无论是私有的还是公共的)很久以前, 我使用它来将我的特定调试器与另一个项目集成在一起。

Yes, it seems to me that it's immutable. 是的,在我看来,这是一成不变的。

Since you have a private field that can only be set in the constructor and the only method in that class is a getter, then that field cannot be modified anymore, and the class can be said immutable. 因为您有一个只能在构造函数中设置的私有字段,并且该类中的唯一方法是getter,所以该字段不能再被修改,并且该类可以说是不可变的。

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

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