📄 chap01.htm
字号:
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I10'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I11>
</FONT><LI><FONT FACE="Verdana"><B> Every
object has a type</B></FONT><FONT FACE="Georgia">. 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?”
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I11'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I12>
</FONT><LI><FONT FACE="Verdana"><B> All
objects of a particular type can receive the same
messages</B></FONT><FONT FACE="Georgia">. 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 most powerful concepts in OOP.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I13>
</FONT><A NAME="_Toc375545189"></A><A NAME="_Toc408018386"></A><A NAME="_Toc472654683"></A><A NAME="_Toc481064468"></A></OL><A NAME="Heading23"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
An object has an interface<BR><A NAME="Index39"></A><A NAME="Index40"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I14>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index41"></A><A NAME="Index42"></A><FONT FACE="Georgia">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
<A NAME="Index43"></A><A NAME="Index44"></A>keyword <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="Index45"></A><A NAME="Index46"></A><I>sending messages</I> or
<A NAME="Index47"></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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I15>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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</FONT><A NAME="fnB3" HREF="#fn3">[3]</A><FONT FACE="Georgia">.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I15'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I16>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since a class describes a set of objects
that have identical characteristics (data elements) and behaviors
(functionality), a class is really a
<A NAME="Index48"></A><A NAME="Index49"></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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I16'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I17'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I18>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I18'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I19>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But how do you get an object to do useful
work for you? There must be a way to make a request of the object so that it
will do something, such as complete a transaction, draw something on the screen,
or turn on a switch. And each object can satisfy only certain requests. The
requests you can make of an object are defined by its <I>interface,</I> and the
type is what determines the interface. A simple example might be a
representation of a light bulb:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I19'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I20>
</FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><IMG SRC="TIJ203.gif"></FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Light lt = <font color=#0000ff>new</font> Light();
lt.on();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The interface establishes <I>what</I>
requests you can make for a particular object. However, there must be code
somewhere to satisfy that request. This, along with the hidden data, comprises
the <A NAME="Index50"></A><I>implementation</I>. From a procedural programming
standpoint, it’s not that complicated. A type has a function associated
with each possible request, and when you make a particular request to an object,
that function is called. This process is usually summarized by saying that you
“send a message” (make a request) to an object, and the object
figures out what to do with that message (it executes
code).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I20'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I21>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here, the name of the type/class is
<B>Light</B>, the name of this particular <B>Light </B>object is <B>lt</B>,<B>
</B>and the requests that you can make of a <B>Light</B> object are to turn it
on, turn it off, make it brighter, or make it dimmer. You create a <B>Light
</B>object by defining a “reference” (<B>lt</B>) for that object and
calling <B>new</B> to request a new object of that type. To send a message to
the object, you state the name of the object and connect it to the message
request with a period (dot). From the standpoint of the user of a predefined
class, that’s pretty much all there is to programming with
objects.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I21'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I22>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The diagram shown above follows the
format of the <A NAME="Index51"></A><A NAME="Index52"></A><I>Unified Modeling
Language</I> (UML). Each class is represented by a box, with the type name in
the top portion of the box, any data members that you care to describe in the
middle portion of the box, and the
<A NAME="Index53"></A><A NAME="Index54"></A><I>member functions</I> (the
functions that belong to this object, which receive any messages you send to
that object) in the bottom portion of the box. Often, only the name of the class
and the public member functions are shown in UML design diagrams, and so the
middle portion is not shown. If you’re interested only in the class name,
then the bottom portion doesn’t need to be shown,
either.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I22'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I23>
</FONT><A NAME="_Toc375545190"></A><A NAME="_Toc408018387"></A><A NAME="_Toc472654684"></A><A NAME="_Toc481064469"></A><BR></P></DIV>
<A NAME="Heading24"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The hidden implementation</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is helpful to break up the playing
field into <A NAME="Index55"></A><I>class creators</I> (those who create new
data types) and <A NAME="Index56"></A><A NAME="Index57"></A><I>client
programmers</I></FONT><A NAME="fnB4" HREF="#fn4">[4]</A><A NAME="Index58"></A><FONT FACE="Georgia">
(the class consumers who use the data types in their applications). The goal of
the client programmer is to collect a toolbox full of classes to use for rapid
application development. The goal of the class creator is to build a class that
exposes only what’s necessary to the client programmer and keeps
everything else hidden. Why? Because if it’s hidden, the client programmer
can’t use it, which means that the class creator can change the hidden
portion at will without worrying about the impact to anyone else. The hidden
portion usually represents the tender insides of an object that could easily be
corrupted by a careless or uninformed client programmer, so hiding the
implementation reduces program bugs. The concept of
<A NAME="Index59"></A><A NAME="Index60"></A>implementation hiding cannot be
overemphasized.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I23'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I24>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In any relationship it’s important
to have boundaries that are respected by all parties involved. When you create a
library, you establish a relationship with the client<I> </I>programmer, who is
also a programmer, but one who is putting together an application by using your
library, possibly to build a bigger
library.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER1_I24'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER1_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If all the members of a class are
available to everyone, then the client programmer can do anything with that
class and there’s no way to enforce rules. Even though you might really
prefer that the client programmer not directly manipulate some of the members of
your class, without access control there’s no way to prevent it.
Everything’s naked to the
world.
</backtalk:display>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -