📄 chapter02.html
字号:
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most procedural languages have the
concept of <I>scope</I>. This determines both the visibility and lifetime of the
names defined within that scope. In C, C++ and Java, scope is determined by the
placement of curly braces <B>{}</B>. So for example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
<font color=#0000ff>int</font> x = 12;
<font color=#009900>/* only x available */</font>
{
<font color=#0000ff>int</font> q = 96;
<font color=#009900>/* both x & q available */</font>
}
<font color=#009900>/* only x available */</font>
<font color=#009900>/* q “out of scope” */</font>
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A variable defined within a scope
is available only to the end of that scope.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Indentation makes Java code easier
to read. Since Java is a free form language, the extra spaces, tabs and carriage
returns do not affect the resulting program.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that you <I>cannot</I> do the
following, even though it is legal in C and C++:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
<font color=#0000ff>int</font> x = 12;
{
<font color=#0000ff>int</font> x = 96; <font color=#009900>/* illegal */</font>
}
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The compiler will announce that the
variable <B>x </B>has already been defined. Thus the C and C++ ability to
“hide” a variable in a larger scope is not allowed because the Java
designers thought that it led to confusing
programs.</FONT><A NAME="_Toc375545224"></A><A NAME="_Toc408018425"></A><BR></P></DIV>
<A NAME="Heading69"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Scope of objects</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java objects do not have the same
lifetimes as primitives. When you create a Java object using <B>new</B>, it
hangs around past the end of the scope. Thus if you use:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
String s = <font color=#0000ff>new</font> String(<font color=#004488>"a string"</font>);
} <font color=#009900>/* end of scope */</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">the handle <B>s</B> vanishes at the
end of the scope. However, the <B>String</B> object that <B>s</B> was pointing
to is still occupying memory. In this bit of code, there is no way to access the
object because the only handle to it is out of scope. In later chapters
you’ll see how the handle to the object can be passed around and
duplicated during the course of a program.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It turns out that because objects
created with <B>new</B> stay around for as long as you want them, a whole slew
of programming problems simply vanish in C++ and Java. The hardest problems seem
to occur in C++ because you don’t get any help from the language in making
sure that the objects are available when they’re needed. And more
importantly, in C++ you must make sure that you destroy the objects when
you’re done with them.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">That brings up an interesting
question. If Java leaves the objects lying around, what keeps them from filling
up memory and halting your program? This is exactly the kind of problem that
would occur in C++. This is where a bit of magic happens. Java has a <I>garbage
collector</I>, which looks at all the objects that were created with <B>new</B>
and figures out which ones are not being referenced anymore. Then it releases
the memory for those objects, so the memory can be used for new objects. This
means that you never need to worry about reclaiming memory yourself. You simply
create objects, and when you no longer need them they will go away by
themselves. This eliminates a certain class of programming problem: the
so-called “memory leak,” in which a programmer forgets to release
memory.</FONT><A NAME="_Toc375545225"></A><A NAME="_Toc408018426"></A><BR></P></DIV>
<A NAME="Heading70"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Creating new <BR>data types: class</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If everything is an object, what
determines how a particular class of object looks and behaves? Put another way,
what establishes the <I>type</I> of an object? You might expect there to be a
keyword called “type” and that certainly would have made sense.
Historically, however, most object-oriented languages have used the keyword
<B>class</B> to mean “I’m about to tell you what a new type of
object looks like.” The <B>class</B> keyword (which is so common that it
will not be emboldened throughout the book) is followed by the name of the new
type. For example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> ATypeName { <font color=#009900>/* class body goes here */</font> }</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This introduces a new type, so you
can now create an object of this type using <B>new</B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>ATypeName a = <font color=#0000ff>new</font> ATypeName();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>ATypeName</B>, the class body
consists only of a comment (the stars and slashes and what is inside, which will
be discussed later in this chapter) so there is not too much that you can do
with it. In fact, you cannot tell it to do much of anything (that is, you cannot
send it any interesting messages) until you define some methods for
it.</FONT><A NAME="_Toc375545226"></A><A NAME="_Toc408018427"></A><BR></P></DIV>
<A NAME="Heading71"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Fields and methods</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you define a class (and all
you do in Java is define classes, make objects of those classes and send
messages to those objects), you can put two types of elements in your class:
data members (sometimes called <I>fields</I>) and member functions (typically
called <I>methods</I>). A data member is an object (that you communicate with
via its handle) of any type. It can also be one of the primitive types (which
isn’t a handle). If it is a handle to an object, you must initialize that
handle to connect it to an actual object (using <B>new</B>, as seen earlier) in
a special function called a <I>constructor</I> (described fully in Chapter 4).
If it is a primitive type you can initialize it directly at the point of
definition in the class. (As you’ll see later, handles can also be
initialized at the point of definition.)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each object keeps its own storage
for its data members; the data members are not shared among objects. Here is an
example of a class with some data members:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> DataOnly {
<font color=#0000ff>int</font> i;
<font color=#0000ff>float</font> f;
<font color=#0000ff>boolean</font> b;
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This class doesn’t <I>do</I>
anything, but you can create an object:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>DataOnly d = <font color=#0000ff>new</font> DataOnly();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can assign values to the data
members, but you must first know how to refer to a member of an object. This is
accomplished by stating the name of the object handle, followed by a period
(dot), followed by the name of the member inside the object
(<B>objectHandle.member</B>). For example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>d.i = 47;
d.f = 1.1f;
d.b = <font color=#0000ff>false</font>;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is also possible that your
object might contain other objects that contain data you’d like to modify.
For this, you just keep “connecting the dots.” For
example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>myPlane.leftTank.capacity = 100;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>DataOnly </B>class cannot do
much of anything except hold data, because it has no member functions (methods).
To understand how those work, you must first understand <I>arguments</I> and
<I>return values</I>, which will be described shortly.</FONT><BR></P></DIV>
<A NAME="Heading72"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Default values for primitive members</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When a primitive data type is a
member of a class, it is guaranteed to get a default value if you do not
initialize it:</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=81 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Primitive type</FONT><BR></P></DIV>
</TH>
<TH WIDTH=84 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Default</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Boolean</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>false</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Char</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">‘\u0000’
(<B>null</B>)</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>byte</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>(byte)0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>short</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>(short)0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>int</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>long</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0L</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>float</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0.0f</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>double</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0.0d</B></FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note carefully that the default
values are what Java guarantees when the variable is used <I>as a member of a
class</I>. This ensures that member variables of primitive types will always be
initialized (something C++ doesn’t do), reducing a source of bugs.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, this guarantee
doesn’t apply to “local” variables – those that are not
fields of a class. Thus, if within a function definition you
have:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> x;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Then <B>x</B> will get some random
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -