Documentation

The Java™ Tutorials
Hide TOC
What Is an Object?什么是对象?
Trail: Learning the Java Language
Lesson: Object-Oriented Programming Concepts

What Is an Object?什么是对象?

Objects are key to understanding object-oriented technology.对象是理解面向对象技术的关键。Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.现在环顾四周,你';I’我会找到许多真实物体的例子:你的狗,你的桌子,你的电视机,你的自行车。

Real-world objects share two characteristics: They all have state and behavior.现实世界的对象有两个共同的特征:它们都有状态行为Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).狗有状态(名称、颜色、品种、饥饿感)和行为(吠叫、抓东西、摇尾巴)。Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).自行车也有状态(当前档位、当前踏板节奏、当前速度)和行为(换档、更改踏板节奏、踩刹车)。Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.识别真实世界对象的状态和行为是从面向对象编程的角度开始思考的好方法。

Take a minute right now to observe the real-world objects that are in your immediate area.现在花一分钟时间观察你周围的真实物体。For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?".对于看到的每个对象,问自己两个问题:“这个对象可能处于什么状态?”和“这个对象可能执行什么行为?”。Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune).一定要写下你的观察结果。正如你所做的,你';我会注意到现实世界中的对象在复杂性上有所不同;桌面指示灯可能只有两种可能的状态(打开和关闭)和两种可能的行为(打开、关闭),但桌面收音机可能有其他状态(打开、关闭、当前音量、当前电台)和行为(打开、关闭、增大音量、减小音量、搜索、扫描和调谐)。You may also notice that some objects, in turn, will also contain other objects.您可能还注意到,某些对象反过来也将包含其他对象。These real-world observations all translate into the world of object-oriented programming.这些真实世界的观察结果都转化为面向对象编程的世界。

A circle with an inner circle filled with items, surrounded by gray wedges representing methods that allow access to the inner circle.

A software object.软件对象。


Software objects are conceptually similar to real-world objects: they too consist of state and related behavior.软件对象在概念上类似于真实世界的对象:它们也由状态和相关行为组成。An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).对象将其状态存储在字段(某些编程语言中的变量)中,并通过方法(某些编程语言中的函数)公开其行为。Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.方法对对象的内部状态进行操作,并作为对象到对象通信的主要机制。Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.隐藏内部状态并要求通过对象的方法执行所有交互称为数据封装—面向对象编程的基本原理。

Consider a bicycle, for example:例如,考虑一辆自行车:

A picture of an object, with bicycle methods and instance variables.

A bicycle modeled as a software object.作为软件对象建模的自行车。


By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it.通过指定状态(当前速度、当前踏板步频和当前档位)并提供更改该状态的方法,对象仍然可以控制外部世界如何使用它。For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.例如,如果自行车只有6个档位,则换档方法可以拒绝小于1或大于6的任何值。

Bundling code into individual software objects provides a number of benefits, including:将代码捆绑到单个软件对象中提供了许多好处,包括:

  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects.模块化:对象的源代码可以独立于其他对象的源代码编写和维护。Once created, an object can be easily passed around inside the system.一旦创建了对象,就可以在系统内轻松地传递对象。
  2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.信息隐藏:通过仅与对象的方法交互,其内部实现的细节对外部世界保持隐藏。
  3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program.代码重用:如果一个对象已经存在(可能是由另一个软件开发人员编写的),您可以在程序中使用该对象。This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.这允许专家实现/测试/调试复杂的、特定于任务的对象,然后您可以信任这些对象在您自己的代码中运行。
  4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement.可插拔性和调试方便性:如果某个特定对象出现问题,您只需将其从应用程序中删除,然后插入另一个对象作为替代对象。This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.这类似于解决现实世界中的机械问题。如果螺栓断裂,则更换螺栓,而不是整个机器。

Previous page: Object-Oriented Programming Concepts
Next page: What Is a Class?