关于JAVA对象的资料翻译

文章地址:来自oracle官方文档,关于JAVA对象的解释
原文:
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.
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.

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:

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.
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.

译文:
什么是对象?
对象是理解面向对象编程技术的关键,现在环顾一下周围,你会发现许多现实世界的对象例子:你的狗,你的桌子,你的电视设备,你的自行车。
现实世界的对象有两个特点:他们都有自己的状态和行为。狗有自己的状态(名字,颜色,品种,饥饿程度)以及行为(叫,摇尾巴)。自行车也有状态(当前的档位、当前的踏板频率、当前的速度)以及行为(改变齿轮转速、改变踏板频率、使用刹车)。辨认现实世界对象的状态和行为是一个从面向对象编程角度开始思考问题的很好的方法。
现在花一分钟时间去观察你附近现实世界的对象。对于每个你见到的对象,问问自己两个问题:“这个对象可能具有什么状态?”以及“这个对象可能会表现出什么行为?”确认然后写下你的观察结果。像你做的一样,你会注意到现实世界的对象复杂性不同;你的台灯可能只有两个属性(开或者关),可能只有两个行为(开,关),但是你桌上的收音机可能还有额外的状态(开,关,当前音量,当前的频道),以及行为(开,关,增加音量,减少音量,搜索频道,扫描,以及调频)。你可能也会注意到一些对象,还包含其他对象。这些现实生活中的观察都能够转变成面向对象编程的世界。
软件对象在概念上也和现实生活中的对象类似,他们也包含了状态和相关联的行为。一个对象的状态存储在字段中(一些编程语言中叫做变量)以及通过方法(一些编程语言中叫做函数)来表现他的行为。方法操作一个对象的内部状态,以及作为对象到对象间联系的主要机制。隐藏内部状态以及要求所有的交互都要通过一个对象的方法执行被称为数据封装——这是面向对象编程的基本原理。
以一辆自行车思考为例:
通过将状态(当前速度,当前踩踏板的节奏,以及当前档位)以及提供改变该状态的方法,这一对象保持着控制外界如何使用它。举个例子,如果这辆自行车只有6个档位,一个方法要能换挡,档位值不能小于1或者大于6。
将代码封装到个人的软件对象里有以下的几个好处,它包括:
1.模块性:一个对象的源码能够独立于其他对象进行编译和维护。只要创建了一次,一个对象就能很容易的在系统中传递
2.隐藏信息:通过相互作用只需要调用一个对象的方法,他的内部细节仍然会对外界隐藏。
3.代码反复利用:如果一个对象已经存在(可能被其他的软件开发者编译过),你也能在你的程序里使用这个对象。这使专家们能够实现、测试、调试复杂的、特殊任务的对象,然后你能够信任这些对象在你的代码中运行。
可插性以及调试简单性:如果一个特别的对象是有问题的,你可以很容易从你的应用中去移除它,以及用不同的对象中作为替代物去替代它。这类似于在现实生活中修理机器故障。如果一个螺丝坏了,换一个,而不用更换整个机器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值