数据封装英文怎么说

Data encapsulation is the most important concept to grasp when programming with objects. In object-oriented programming data encapsulation is concerned with:

使用对象进行编程时,数据封装是最重要的概念。 在面向对象的编程中,数据封装与以下方面有关:

  • Combining data and how it's manipulated in one place. This is achieved through the state (the private fields) and the behaviors (the public methods) of an object. 组合数据以及如何在一处处理数据。 这是通过对象的状态(私有字段)和行为(公共方法)实现的。
  • Only allowing the state of an object to be accessed and modified through behaviors. The values contained within an object's state can then be strictly controlled. 仅允许通过行为访问和修改对象的状态。 然后可以严格控制对象状态中包含的值。
  • Hiding the details of how the object works. The only part of the object that is accessible to the outside world is its behaviors. What happens inside those behaviors and how the state is stored is hidden from view. 隐藏对象工作原理的细节。 外界可以访问的对象的唯一部分是其行为。 这些行为内部发生的事情以及状态的存储方式都无法看到。

( Enforcing Data Encapsulation )

First, we must design our objects so that they have state and behaviors. We create private fields that hold the state and public methods that are the behaviors.

首先,我们必须设计对象,使其具有状态和行为。 我们创建包含状态的私有字段和作为行为的公共方法。

For example, if we design a person object we can create private fields to store a person's first name, last name, and address. The values of these three fields combine to make the object's state. We could also create a method called displayPersonDetails to display the values of the first name, last name, and address to the screen.

例如,如果我们设计一个人员对象,则可以创建私有字段来存储人员的名字,姓氏和地址。 这三个字段的值组合在一起形成对象的状态。 我们还可以创建一个名为displayPersonDetails的方法,以在屏幕上显示名字,姓氏和地址的值。

Next, we must make behaviors that access and modify the state of the object. This can be accomplished in three ways:

接下来,我们必须做出访问和修改对象状态的行为。 这可以通过三种方式完成:

  • Constructor methods. A new instance of an object is created by calling a constructor method. Values can be passed to a constructor method to set the initial state of an object. There are two interesting things to note. First, Java does not insist that every object has a constructor method. If no method exists then the state of the object uses the default values of the private fields. Second, more than one constructor method can exist. The methods will differ in terms of the values that are passed to them and how they set the initial state of the object. 构造方法。 通过调用构造方法创建对象的新实例。 可以将值传递给构造函数方法以设置对象的初始状态。 有两个有趣的事情要注意。 首先, Java并不坚持每个对象都有构造方法。 如果不存在任何方法,则对象的状态将使用私有字段的默认值。 其次,可以存在多个构造方法。 这些方法在传递给它们的值以及它们如何设置对象的初始状态方面会有所不同。
  • Accessor methods. For every private field we can create a public method that will return its value. 访问器方法。 对于每个私有字段,我们可以创建一个将返回其值的公共方法。
  • Mutator methods. For every private field we can create a public method that will set its value. If you want a private field to be read only do not create a mutator method for it. 变异器方法。 对于每个私有字段,我们可以创建一个公共方法来设置其值。 如果您希望私有字段是只读的,请不要为其创建一个mutator方法。

For example, we can design the person object to have two constructor methods. The first one doesn't take any values and simply sets the object to have a default state (i.e., the first name, last name, and address would be empty strings). The second one sets the initial values for the first name and last name from values passed to it. We can also create three accessor methods called getFirstName, getLastName and getAddress that simply return the values of the corresponding private fields. Create a mutator field called setAddress that will set the value of the address private field.

例如,我们可以将person对象设计为具有两个构造方法。 第一个不带任何值,只是将对象设置为默认状态(即,名字,姓氏和地址为空字符串)。 第二个从传递给它的值中设置名字和姓氏的初始值。 我们还可以创建三个访问器方法,分别称为getFirstName,getLastName和getAddress,它们仅返回相应私有字段的值。 创建一个名为setAddress的mutator字段,该字段将设置地址私有字段的值。

Lastly, we hide the implementation details of our object. As long as we stick to keeping the state fields private and the behaviors public there is no way for the outside world to know how the object works internally.

最后,我们隐藏对象的实现细节。 只要我们坚持将状态字段设为私有,并将行为公开化,外界就无法知道对象在内部如何工作。

( Reasons for Data Encapsulation )

The main reasons for employing data encapsulation are:

采用数据封装的主要原因是:

  • Keeping the state of an object legal. By forcing a private field of an object to be modified by using a public method, we can add code into the mutator or constructor methods to make sure the value is legal. For instance, imagine the person object also stores a username as part of its state. The username is used to log into the Java application we're building but is constrained to a length of ten characters. What we can do is add code into the username's mutator method that makes sure the username is not set to a value longer than ten characters. 保持对象状态合法。 通过强制使用公共方法修改对象的私有字段,我们可以将代码添加到mutator或构造方法中,以确保该值合法。 例如,假设人员对象还存储用户名作为其状态的一部分。 用户名用于登录我们正在构建的Java应用程序,但长度不能超过10个字符。 我们可以做的是将代码添加到用户名的mutator方法中,以确保用户名的值不超过十个字符。
  • We can change the implementation of an object. As long as we keep the public methods the same we can change how the object works without breaking the code that uses it. The object is essentially a "black box" to the code that calls it. 我们可以更改对象的实现。 只要我们保持公共方法不变,就可以更改对象的工作方式而不会破坏使用它的代码。 该对象实质上是调用它的代码的“黑匣子”。
  • Re-use of objects. We can use the same objects in different applications because we have combined the data and how it's manipulated in one place. 重用对象。 我们可以在不同的应用程序中使用相同的对象,因为我们已经组合了数据及其在一个地方的处理方式。
  • The independence of each object. If an object is incorrectly coded and causing errors, it's easy to test and fix because the code is in one place. In fact, the object can be tested independently from the rest of the application. The same principle can be used in large projects where different programmers can be assigned the creation of different objects. 每个对象的独立性。 如果一个对象的编码不正确并导致错误,则由于代码位于一个位置,因此很容易测试和修复。 实际上,可以与应用程序的其余部分无关地测试对象。 可以在大型项目中使用相同的原理,在大型项目中,可以为不同的程序员分配不同的对象。

翻译自: https://www.thoughtco.com/data-encapsulation-2034263

数据封装英文怎么说