java中怎么访问局部变量
One of the ways we can enforce data encapsulation is through the use of accessors and mutators. The role of accessors and mutators are to return and set the values of an object's state. Let's learn how to program accessors and mutators in Java. As an example, we'll use a Person class with the state and constructor already defined:
我们可以执行数据封装的方法之一是使用访问器和更改器。 访问器和更改器的作用是返回并设置对象状态的值。 让我们学习如何用Java编程访问器和变异器。 举个例子,我们将使用一个Person类 ,其状态和构造函数已经定义:
( Accessor Methods )
An accessor method is used to return the value of a private field. It follows a naming scheme prefixing the word "get" to the start of the method name. For example let's add accessor methods for firstname, middleNames and lastname:
访问器方法用于返回私有字段的值。 它遵循一种命名方案,在方法名称的开头加上单词“ get”。 例如,让我们为名,中间名和姓添加访问器方法:
These methods always return the same data type as their corresponding private field (e.g., String) and then simply return the value of that private field.
这些方法总是返回与其对应的私有字段相同的数据类型(例如String),然后简单地返回该私有字段的值。
We can now access their values through the methods of a Person object:
现在,我们可以通过Person对象的方法访问它们的值:
( Mutator Methods )
A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name. For example, let's add mutator fields for address and username:
mutator方法用于设置私有字段的值。 它遵循一种命名方案,在方法名称的开头加上单词“ set”。 例如,让我们为地址和用户名添加变量字段:
These methods do not have a return type and accept a parameter that is the same data type as their corresponding private field. The parameter is then used to set the value of that private field.
这些方法没有返回类型,并接受与其对应的专用字段相同的数据类型的参数。 然后,该参数用于设置该私有字段的值。
It's now possible to modify the values for the address and username inside the Person object:
现在可以在Person对象中修改地址和用户名的值:
( Why Use Accessors and Mutators? )
It's easy to come to the conclusion that we could just change the private fields of the class definition to be public and achieve the same results. It's important to remember that we want to hide the data of the object as much as possible. The extra buffer provided by these methods allows us to:
很容易得出这样的结论:我们可以将类定义的私有字段更改为公共字段,并获得相同的结果。 重要的是要记住,我们要尽可能隐藏对象的数据。 这些方法提供的额外缓冲区使我们能够:
- Change how the data is handled behind the scenes.
- Impose validation on the values that the fields are being set to.
Let's say we decide to modify how we store middle names. Instead of just one String we can now use an array of Strings:
假设我们决定修改存储中间名的方式。 现在,我们不仅可以使用一个字符串,而且可以使用字符串数组:
The implementation inside the object has changed but the outside world is not affected. The way the methods are called remains exactly the same:
对象内部的实现已更改,但外部世界不受影响。 方法的调用方式完全相同:
Or, let's say the application that is using the Person object can only accept usernames that have a maximum of ten characters. We can add validation in the setUsername mutator to make sure the username conforms to this requirement:
或者,假设使用Person对象的应用程序只能接受最多包含十个字符的用户名。 我们可以在setUsername mutator中添加验证,以确保用户名符合此要求:
Now if the username passed to the setUsername mutator is longer than ten characters it is automatically truncated.
现在,如果传递给setUsername mutator的用户名超过10个字符,它将自动被截断。
翻译自: https://www.thoughtco.com/accessors-and-mutators-2034335
java中怎么访问局部变量