- 论坛徽章:
- 0
|
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.
![]()
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.
Bundling code into individual software objects provides a number of benefits, including:
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.
Information-hiding: By interacting only with an object's
methods, the details of its internal implementation remain hidden from
the outside world.
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.
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.
##################################################################################
上面是原文,为了阅读方便,我把它翻译如下(如有不当的地方,欢迎指出):
"对象"概念是理解面向对象技术的关键,现在看看你周围,便会发现现实世界中好多例子:你的狗,你的桌子,你家的电视,你的自行车.
现实世界中的对象通常有两个特性:它们都具有状态和行为.狗有(名字,颜色,繁殖,饥饿)状态和(,犬叫,获取东西,要尾巴)行为.自行车同样具有(当前的齿轮,当前的脚踏板,当前的速度)状态和(改变当前的齿轮,改变当前的脚踏板,改变当前的速度)行为.识别现实世界对象的状态和行为是开始思考面向对象编程的很好方法。
现在花一分钟去观察下你周围的对象,在你看到的每个对象,问自己两个问题:
"这个对象可能的状态是什么?"和"这个对象能够执行的动作是什么?".
确定你写下你所观察的对象,正如你所做的那样,你将注意到现实世界中的对象复杂度很是不同的。你的台灯对象可能仅仅有2个可能的状态(开和关)和两个可能的行为(打开和关闭).但是你的收音机可能附加的状态(开,关,当前的音量,当前的电台)和行为(打开,关闭,增加音量,降低音量,查找,扫描和曲调).你可能注意到有些对象,可能包含着其它的对象,这些现实世界的对象都能转换成面向对象编程的对象。
![]()
软件对象.
软件对象和真实世界的对象在概念上挺相似的。它们都是有状态和行为组成的。对象在他的属性里面存储它的状态(程序语言的变量)和调用他的行为通过方法(一些编程语言的函数).
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.
Bundling code into individual software objects provides a number of benefits, including:
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.
Information-hiding: By interacting only with an object's
methods, the details of its internal implementation remain hidden from
the outside world.
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.
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.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/24141/showart_323737.html |
|