📄 ch3.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 3 -- Object-Oriented Programming with Java</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 3</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Object-Oriented Programming with Java</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#WhatIsOOP" >What Is OOP?</A>
<UL>
<LI><A HREF="#Objects" >Objects</A>
<LI><A HREF="#Classes" >Classes</A>
<LI><A HREF="#Encapsulation" >Encapsulation</A>
<LI><A HREF="#Messages" >Messages</A>
<LI><A HREF="#Inheritance" >Inheritance</A>
</UL>
<LI><A HREF="#OOPandGames" >OOP and Games</A>
<LI><A HREF="#JavaandOtherOOPLanguages" >Java and Other OOP Languages</A>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
<I>Object-oriented programming</I> (OOP) is a programming paradigm
that is fundamentally different from traditional procedural programming
styles. It is centered around the concept of objects-programming
constructs that have both properties and the procedures for manipulating
those properties. This approach models the real world much more
closely than conventional programming methods and is ideal for
the simulation-type problems commonly encountered in games.
<P>
You're probably already aware that Java is an object-oriented
language, but you might not fully understand what that means.
To successfully use Java to write Internet games, you need to
embrace object-oriented programming techniques and design philosophies.
The goal of today's lesson is to present the conceptual aspects
of object-oriented programming as they relate to Java. By the
end of today's lesson, you will fully understand what OOP means
to Java and maybe even have some new buzz words to share with
your friends! More important, you will gain some insight into
why the OOP paradigm built into Java is a perfect match for game
programming.
<P>
The following topics are covered in today's lesson:
<UL>
<LI>What is OOP?
<LI>OOP and games
<LI>Java and other OOP languages
</UL>
<H2><A NAME="WhatIsOOP"><B><FONT SIZE=5 COLOR=#FF0000>What Is
OOP?</FONT></B></A></H2>
<P>
If you've been anywhere near the computer section of a bookstore
or picked up a programming magazine in the last five years, you've
certainly seen the hype surrounding object-oriented programming.
It's the most popular programming technology to come about in
a long time, and it all revolves around the concept of an object.
The advent of Java has only served to elevate the hype surrounding
OOP. You might wonder what the big deal is with objects and object-oriented
technology? Is it something you should be concerned with, and
if so, why? Is it really that crucial when working with Java?
If you sift through the hype surrounding the whole object-oriented
issue, you'll find a very powerful technology that provides a
lot of benefits to software design.
<P>
But the question still remains: What is OOP? OOP is an approach
to programming that attempts to bridge the gap between problems
in the real world and solutions in the computer programming world.
Prior to OOP, a conceptual stumbling block always existed for
programmers when trying to adapt the real world into the constraints
imposed by a traditional programming language. In the real world,
people tend to think in terms of "things," but in the
pre-OOP programming world people have been taught to think in
terms of blocks of code (procedures) and how they act on data.
These two modes of thinking are very different from each other
and pose a significant problem when it comes to designing complex
systems that model the real world. Games happen to be very good
examples of complex systems that often model the real world.
<P>
OOP presents an approach to programming that allows programmers
to think in terms of objects, or things, much like people think
of things in the real world. Using OOP techniques, a programmer
can focus on the objects that naturally make up a system, rather
than trying to rationalize the system into procedures and data.
The OOP approach is a very natural and logical application of
the way humans already think.
<P>
The benefits of OOP go beyond easing the pain of resolving real
world problems in the computer domain. Another key issue in OOP
is code reuse, when you specifically design objects and programs
with the goal of reusing as much of the code as possible, whenever
possible. Fortunately, it works out that the fundamental approaches
to OOP design naturally encourage code reuse, meaning that it
doesn't take much of an extra effort to reuse code after you employ
standard OOP tactics.
<P>
The OOP design approach revolves around the following major concepts:
<UL>
<LI>Objects
<LI>Classes
<LI>Encapsulation
<LI>Messages
<LI>Inheritance
</UL>
<H3><A NAME="Objects"><B>Objects</B></A></H3>
<P>
<I>Objects</I> are bundles of data and the code, or procedures,
that act on that data.
<P>
The procedures in an object are also known as <I>methods</I>.
The merger of data and methods provides a means of more accurately
representing real-world objects. Modeling a real-world problem
through traditional programming constructs, without objects, requires
a significant logical leap. Objects, on the other hand, enable
programmers to solve real-world problems in the software domain
much more easily and logically.
<P>
As evident by the name, objects are at the heart of object-oriented
technology. To understand how software objects are beneficial,
think about the common characteristics of all real-world objects.
Lions, cars, and calculators all share two common characteristics:
state and behavior.
<P>
The <I>state</I> of an object is the condition that the object
is in, as defined by its attributes.
<P>
The <I>behavior</I> of an object is the collection of actions
that the object can take.
<P>
For example, the state of a lion might include color, weight,
and whether the lion is tired or hungry. Lions also have certain
behaviors such as roaring, sleeping, and hunting. The state of
a car includes the current speed, the type of transmission, whether
it is two- or four-wheel-drive, whether the lights are on, and
the current gear, among other things. The behaviors for a car
include turning, braking, and accelerating.
<P>
Just like real-world objects, software objects possess two common
characteristics: state and behavior. To relate this back to programming
terms, the <I>state</I> of an object is determined by its data
and the <I>behavior</I> of an object is defined by its methods.
By making this connection between real-world objects and software
objects, you begin to see how objects help bridge the gap between
the real world and the world of software living inside your computer.
<P>
Because software objects are modeled after real-world objects,
you can more easily represent real-world objects in object-oriented
programs. You could use the lion object to represent a real lion
in an interactive software zoo. Similarly, car objects would be
very useful in a racing game. However, you don't always have to
think of software objects as modeling physical real-world objects;
software objects can be just as useful for modeling abstract concepts.
For example, the standard Java API provides a thread object that
represents a stream of execution in a multithreaded program.
<P>
Figure 3.1 shows a visualization of a Java software object, including
the primary components and how they relate.
<P>
<A HREF="f3-1.gif" ><B>Figure 3.1 : </B><I>A software object.</I></A>
<P>
The software object in Figure 3.1 clearly shows the two primary
components of an object: data and methods. The figure also shows
some type of communication, or access, between the data and the
methods. Additionally, it shows how messages are sent through
the methods, which result in responses from the object. You'll
learn more about messages later today in the "Messages"
section.
<P>
The data and methods within an object express everything that
the object knows (state), along with what all it can do (behavior).
A software object modeling a real-world car would have variables
(data) that indicate the car's current state: it's traveling at
75 mph, it is in 4th gear, and the lights are on. The software
car object would also have methods that enable it to brake, accelerate,
steer, change gears, and turn the lights on and off. Figure 3.2
shows what a Java car object might look like.
<P>
<A HREF="f3-2.gif" ><B>Figure 3.2 : </B><I>A car object.</I></A>
<P>
In both Figures 3.1 and 3.2 you probably noticed the line separating
the methods from <BR>
the data within the object. This line is a little misleading,
because methods have full access to the data within an object.
The line is there to illustrate the difference between the visibility
of the methods and the data to the outside. In this sense, an
object's visibility refers to what parts of the object another
object has access to. Because object data defaults to being invisible,
or inaccessible to other objects, all interaction between objects
must be handled via methods. This hiding of data within an object
is called <I>encapsulation</I>.
<H3><A NAME="Classes"><B>Classes</B></A></H3>
<P>
Throughout this discussion of object-oriented programming, you've
only dealt with the concept of an object already existing in a
system. You might be wondering how objects get into a system in
the first place. This question brings you to the most fundamental
structure in object-oriented programming: the class.
<P>
A <I>class</I> is a template or prototype that defines a type
of object.
<P>
A class is to an object what a blueprint is to a house. Many houses
can be built from a single blueprint; the blueprint outlines the
makeup of the houses. Classes work exactly the same way, except
that they outline the makeup of objects.
<P>
In the real world, there are often many objects of the same kind.
Using the house analogy, there are many different houses around
the world, but as houses they all share common characteristics.
In object-oriented terms, you would say that your house is a specific
instance of the class of objects known as houses.
<P>
An <I>instance</I> of a class is an object that has been created
in memory using the class as a template. Instances are also sometimes
referred to as <I>instantiated objects</I>.
<P>
All houses have states and behaviors in common that define them
as houses. When a builder starts building a new development of
houses, he or she typically will build them all from a set of
blueprints. It wouldn't be as efficient to create a new blueprint
for every single house, especially when there are so many similarities
shared between each one. The same thing applies in object-oriented
software development; why rewrite a lot of code when you can reuse
code that solves similar problems?
<P>
In object-oriented programming, as in construction, it's also
common to have many objects of the same kind that share similar
characteristics. And like the blueprints for similar houses, you
can create blueprints for objects that share certain characteristics.
What it boils down to is that classes are software blueprints
for objects.
<P>
As an example, the class for the car object discussed earlier
would contain several variables representing the state of the
car, along with implementations for the methods that enable the
driver to control the car. The state variables of the car remain
hidden underneath the interface. Each instance, or instantiated
object, of the car class gets a fresh set of state variables.
This brings you to another important point: When an instance of
an object is created from a class, the variables declared by that
class are allocated in memory. The variables are then modified
via the object's methods. Instances of the same class share method
implementations but have their own <I>object data</I>. Classes
can also contain class data.
<P>
<I>Object data</I>, or <I>instance data</I>, is the information
that models an object's state. Each object in memory has its own
set of instance data, which determines what state the object is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -