如何在Java中声明一个不能改变类成员的方法? [英] How to declare a method that cannot change the class members in Java?

查看:120
本文介绍了如何在Java中声明一个不能改变类成员的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C++中我们做一个常量方法,所以它不能改变类成员的值,但是我怎么能在Java中做到呢?我尝试了 final 方法声明,它等同于 C++ 中的 consts,但 final 方法是另一回事.我想做一个getter方法,所以它不能改变值,只能读取它.就像一个只读方法.

In C++ we do a constant method, and so it can't change the value of the class members, but how can I do it in Java? I tried the final method declaration, which would be the equivalent to the consts in C++, but final methods are another different thing. I want to make a getter method, so it cannot change the values, only read it. Like a read-only method.

推荐答案

在 Java 中,不可能以声明方式阻止方法更改非 final 字段.没有常量正确性"这样的东西.在 Java 中.

In Java, it's not possible to declaratively prevent a method from changing non-final fields. There's no such thing as "const correctness" in Java.

如果一个类字段是非final,那么它可以被类的任何方法更改.

If a class field is non-final, then it can be changed by any method of the class.

请注意,final 在字段和变量与方法和类上的工作方式不同:

Note that final works differently on fields and variables versus methods and classes:

  • final 字段或变量是一个常量.其值一经分配便无法更改.
  • final 方法不能被子类覆盖.方法上的 final 与constness"无关.
  • 不能扩展 final 类.类的 final 与constness"无关.
  • A final field or variable is a constant. Its value cannot be changed once assigned.
  • A final method cannot be overridden by child classes. final on methods has nothing to do with "constness".
  • A final class cannot be extended. final on classes has nothing to do with "constness".

您在 Java 中的选项是:

Your options in Java are:

  1. 使您的类不可变(即标记其所有字段 final).
  2. 只需仔细编写您的 getter(和其他非修改器方法):)


还有一点.即使在字段和变量上,Java 的 final 也不同于 C++ 的 const.例如:


One more note. Even on fields and variables, Java's final differs from C++'s const. For example:

  • final 变量一旦赋值就不能改变,但是你可以在声明之后赋值.示例:

  • final variables can't be changed once assigned, but you can assign them after the declaration. Example:

  final int value;
  if (condition) {
      value = 1; // Ok!
  } else {
      value = 2; // Ok!
  }
  value = 3; // Compile error: value already assigned.

  • final 必须分配类字段 - 直接分配或从构造函数内分配.但是这些字段可以在构造函数中任何地方分配.没有像 C++ 的初始化列表"那样特殊的东西.在 Java 中.

  • final class fields must be assigned - either directly or from within a constructor. But these fields may be assigned anywhere within a constructor. There's no special thing like C++'s "initializer list" in Java.

    Java 中的字段/变量/参数声明 final Object obj 大致相当于 C++ 中的 (const-) 对非常量对象的引用.这意味着,您不能重新分配引用,但您可以更改引用的对象本身.一个例子:

    The field/variable/parameter declaration final Object obj in Java is roughly equivalent to a (const-)reference to a non-const object in C++. This means, you can't re-assign the reference, but you can change the referenced object itself. An example:

      // Note that (obsolete) Date class is mutable in Java.
      final Date myDate = new Date();
    
      myDate = new Date(); // Compilation error: can't reassign a final reference!
    
      myDate.setTime(4711); // Ok, mutating referenced object is allowed!
    

  • 这篇关于如何在Java中声明一个不能改变类成员的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆