📄 tij303.htm
字号:
problem you’re trying to solve (dogs, buildings, services, etc.) and
represent it as an object in your program. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_111" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>A program is a bunch of objects telling each other what to do by sending
messages</b>. To make a request of an object, you “send a message”
to that object. More concretely, you can think of a message as a request to call
a method that belongs to a particular object. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_112" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>Each object has its own memory made up of other objects</b>. Put another
way, you create a new kind of object by making a package containing existing
objects. Thus, you can build complexity into a program while hiding it behind
the simplicity of objects. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_113" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>Every object has a type</b>. Using the parlance, each object is an
<i>instance</i> of a <i>class</i>, in which “class” is synonymous
with “type.” The most important distinguishing characteristic of a
class is “What messages can you send to it?” <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_114" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>All objects of a particular type can receive the same messages</b>. This
is actually a loaded statement, as you will see later. Because an object of type
“circle” is also an object of type “shape,” a circle is
guaranteed to accept shape messages. This means you can write code that talks to
shapes and automatically handle anything that fits the description of a shape.
This <i>substitutability</i> is one of the powerful concepts in OOP. <font
size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_115"
title="Send BackTalk
Comment">Feedback</a></font></li></ol><p><a name="_Toc375545189"></a><a name="_Toc408018386"></a><a name="_Toc472654683"></a>Booch offers an even more succinct description of an object:<br></p>
<p class="heading 7"><i>An object has state, behavior and identity.</i><br></p>
<p>This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other object—to put this in a concrete sense, each object has a unique address in memory.<sup><a name="fnB3" href="#fn3">[3]</a></sup> <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0105" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc24775514"></a><a name="Heading1012"></a>An object has an
interface<br></h2>
<p><a name="Index43"></a><a name="Index44"></a>Aristotle was probably the first to begin a careful study of the concept of <i>type;</i> he spoke of “the class of fishes and the class of birds.” The idea that all objects, while being unique, are also part of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword <b>class</b> that introduces a new type into a program. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_116" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index45"></a><a name="Index46"></a>Simula, as its name implies, was created for developing simulations such as the classic “bank teller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, and units of money—a lot of “objects.” Objects that are identical except for their state during a program’s execution are grouped together into “classes of objects” and that’s where the keyword <a name="Index47"></a><b>class</b> came from. Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called <i>objects </i>or <i>instances</i> in object-oriented parlance) and manipulate those variables (called <a name="Index48"></a><a name="Index49"></a><i>sending messages</i> or <a name="Index50"></a><i>requests;</i> you send a message and the object figures out what to do with it). The members (elements) of each class share some commonality: every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state: each account has a different balance, each teller has a name. Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_117" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.<sup><a name="fnB4" href="#fn4">[4]</a></sup> <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_118" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Since a class describes a set of objects that have identical characteristics (data elements) and behaviors (functionality), a class is really a data type because a floating point number, for example, also has a set of characteristics and behaviors. The difference is that a programmer defines a class to fit a problem rather than being forced to use an existing data type that was designed to represent a unit of storage in a machine. You extend the programming language by adding new data types specific to your needs. The programming system welcomes the new classes and gives them all the care and type-checking that it gives to built-in types. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_119" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index51"></a><a name="Index52"></a>The object-oriented approach is not limited to building simulations. Whether or not you agree that any program is a simulation of the system you’re designing, the use of OOP techniques can easily reduce a large set of problems to a simple solution. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_120" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Once a class is established, you can make as many objects of that class as you like, and then manipulate those objects as if they are the elements that exist in the problem you are trying to solve. Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space and objects in the solution space. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap01_121" title="Send BackTalk Comment">Feedback</a></font><br></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -