我可以在Java中创建只读属性吗?

时间:2022-09-12 08:43:58

I'm creating a custom array class, and it has an int property size.

我创建了一个自定义数组类,它有一个int属性大小。

This property is modified automatically if you do something like add() or remove(). I want to let the user read this property like myArray.size. However, making it public allows the user to change it, which doesn't make much sense for my class, and could break it.

如果您执行add()或remove()之类的操作,该属性将被自动修改。我想让用户读取myArray.size这样的属性。但是,让它公开允许用户更改它,这对我的类来说没有多大意义,并且可能会破坏它。

I could of course make it private and create a getter method size() and all's good. I don't have a problem with that, but still, is there a way to make a read-only property in Java?

当然,我可以将其设置为私有并创建一个getter方法大小()和所有的优点。我没有问题,但是,是否有办法在Java中创建一个只读属性?

Looking around the web, I found this:

环顾网络,我发现:

Their solution goes like

他们的解决方案是

public final String title;

Which is tecnically valid, but that of course doesn't work for my array, since my property is not constant.

这在构造上是有效的,但这对我的数组不适用,因为我的属性不是常量。

5 个解决方案

#1


2  

What you're describing is not possible in Java. You should use a getter method.

您所描述的在Java中是不可能的。您应该使用getter方法。

#2


2  

I think what you want is for the user to be able to get the object's size but not be able to write to it. In this case, you keep the value of size as private. You are absolutely right that making size public will allow a user to break your objects by setting the value of size to something that it is not!

我认为你想要的是用户能够得到对象的大小但不能写入。在这种情况下,您将大小的值保持为私有。您绝对正确,让大小公开允许用户通过设置大小的值来破坏您的对象。

What you want is a getSize() method that is public and return ' s the value of size. The user can call the method to read the value, but has no way of actually writing to the value of size.

您需要的是一个getSize()方法,它是public的,返回的是size的值。用户可以调用该方法来读取该值,但实际上无法写入大小的值。

If you really want to learn about making part or all of your data structure immutable, you could check here:

如果您真的想学习如何使您的数据结构成为不可变的部分或全部,您可以在这里查看:

Immutable Objects http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

不可变对象http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

#3


1  

Make use of getters/setters. Or rather, just getters without the setters in your case.

使用getter / setter。或者更确切地说,只是在你的案例中没有setter的getter。

Look for an example here: http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

这里有一个例子:http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html。

So make the property private, and create a public method to 'get' its value.

因此,让属性私有,并创建一个公共方法来“获取”它的值。

#4


0  

If you want to make a read only array, you can use ImmutableList : http://docs.oracle.com/cd/E15523_01/apirefs.1111/e13403/oracle/javatools/util/ImmutableList.html

如果您想要创建一个只读数组,那么可以使用ImmutableList: http://docs.oracle.com/cd/E15523_01/apirefs.1111/e13403/oracle/javatools/util/ImmutableList.html。

#5


0  

A simple getter is the natural approach, as Java lacks support for properties. So, a getter is what the typical Java programmer would expect.

简单的getter是自然方法,因为Java缺乏对属性的支持。因此,getter是典型的Java程序员所期望的。

Even if it is possible to use public fields, as in your public final String title example, I would not recommend it. For example, it is annoying when you give your code to someone else who wants to write unit tests for it and uses mocking frameworks.

即使可以使用公共字段,如在您的公共最终字符串标题示例中,我也不会推荐它。例如,当您将代码交给其他想要编写单元测试并使用mock框架的人时,这是很烦人的。

Public fields should generally be reserved for (static) constants.

公共字段一般应保留为(静态)常量。

#1


2  

What you're describing is not possible in Java. You should use a getter method.

您所描述的在Java中是不可能的。您应该使用getter方法。

#2


2  

I think what you want is for the user to be able to get the object's size but not be able to write to it. In this case, you keep the value of size as private. You are absolutely right that making size public will allow a user to break your objects by setting the value of size to something that it is not!

我认为你想要的是用户能够得到对象的大小但不能写入。在这种情况下,您将大小的值保持为私有。您绝对正确,让大小公开允许用户通过设置大小的值来破坏您的对象。

What you want is a getSize() method that is public and return ' s the value of size. The user can call the method to read the value, but has no way of actually writing to the value of size.

您需要的是一个getSize()方法,它是public的,返回的是size的值。用户可以调用该方法来读取该值,但实际上无法写入大小的值。

If you really want to learn about making part or all of your data structure immutable, you could check here:

如果您真的想学习如何使您的数据结构成为不可变的部分或全部,您可以在这里查看:

Immutable Objects http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

不可变对象http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

#3


1  

Make use of getters/setters. Or rather, just getters without the setters in your case.

使用getter / setter。或者更确切地说,只是在你的案例中没有setter的getter。

Look for an example here: http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

这里有一个例子:http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html。

So make the property private, and create a public method to 'get' its value.

因此,让属性私有,并创建一个公共方法来“获取”它的值。

#4


0  

If you want to make a read only array, you can use ImmutableList : http://docs.oracle.com/cd/E15523_01/apirefs.1111/e13403/oracle/javatools/util/ImmutableList.html

如果您想要创建一个只读数组,那么可以使用ImmutableList: http://docs.oracle.com/cd/E15523_01/apirefs.1111/e13403/oracle/javatools/util/ImmutableList.html。

#5


0  

A simple getter is the natural approach, as Java lacks support for properties. So, a getter is what the typical Java programmer would expect.

简单的getter是自然方法,因为Java缺乏对属性的支持。因此,getter是典型的Java程序员所期望的。

Even if it is possible to use public fields, as in your public final String title example, I would not recommend it. For example, it is annoying when you give your code to someone else who wants to write unit tests for it and uses mocking frameworks.

即使可以使用公共字段,如在您的公共最终字符串标题示例中,我也不会推荐它。例如,当您将代码交给其他想要编写单元测试并使用mock框架的人时,这是很烦人的。

Public fields should generally be reserved for (static) constants.

公共字段一般应保留为(静态)常量。