The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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 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 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:将代码捆绑到单个软件对象中提供了许多好处,包括: