⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap04.htm

📁 java书籍《thinking in java》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:07
Translation Platform:Win32
Number of Output files:23
This File:Chap04.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>4: Initialization  &amp; Cleanup</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</a> ]
    [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
    [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/Services">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chap03.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="Chap05.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_4"></A><A NAME="_Toc375545274"></A><A NAME="_Toc477690724"></A><A NAME="_Toc481064568"></A><A NAME="Heading163"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
4: Initialization <BR>&amp; Cleanup</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER4_I0>
As the
computer revolution progresses, &#8220;unsafe&#8221; programming has become one
of the major culprits that makes programming expensive.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Two of these safety issues are
<I>initialization</I> and <I>cleanup</I>. Many C bugs occur when the programmer
forgets to initialize a variable. This is especially true with libraries when
users don&#8217;t know how to initialize a library component, or even that they
must. Cleanup is a special problem because it&#8217;s easy to forget about an
element when you&#8217;re done with it, since it no longer concerns you. Thus,
the resources used by that element are retained and you can easily end up
running out of resources (most notably, memory). 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C++ introduced the concept of a
<I>constructor</I>, a special method automatically called when an object is
created. Java also adopted the constructor, and in addition has a garbage
collector that automatically releases memory resources when they&#8217;re no
longer being used. This chapter examines the issues of initialization and
cleanup, and their support in Java.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I2>
</FONT><A NAME="_Toc312373853"></A><A NAME="_Toc375545275"></A><A NAME="_Toc481064569"></A><BR></P></DIV>
<A NAME="Heading164"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Guaranteed initialization <BR>with the constructor</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can imagine creating a method called
<B>initialize(&#160;)</B> for every class you write. The name is a hint that it
should be called before using the object. Unfortunately, this means the user
must remember to call the method. In Java, the class designer can guarantee
initialization of every object by providing a special method called a
<I>constructor<A NAME="Index380"></A><A NAME="Index381"></A></I>. If a class has
a constructor, Java automatically calls that constructor when an object is
created, before users can even get their hands on it. So initialization is
guaranteed. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The next challenge is what to name this
method. There are two issues. The first is that any name you use could clash
with a name you might like to use as a member in the class. The second is that
because the compiler is responsible for calling the constructor, it must always
know which method to call. The C++ solution seems the easiest and most logical,
so it&#8217;s also used in Java: the name of the constructor
<A NAME="Index382"></A>is the same as the name of the class. It makes sense that
such a method will be called automatically on initialization.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s a simple class with a
constructor:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:SimpleConstructor.java</font>
<font color=#009900>// Demonstration of a simple constructor.</font>

<font color=#0000ff>class</font> Rock {
  Rock() { <font color=#009900>// This is the constructor</font>
    System.out.println(<font color=#004488>"Creating Rock"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> SimpleConstructor {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; 10; i++)
      <font color=#0000ff>new</font> Rock();
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now, when an
<A NAME="Index383"></A>object is created:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I5>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>new</font> Rock();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">storage is allocated and the constructor
is called. It is guaranteed that the object will be properly initialized before
you can get your hands on it.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I5' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I6>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that the coding style of making the
first letter of all methods lowercase does not apply to constructors, since the
name of the constructor must match the name of the class <I>exactly</I>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I6' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I7>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Like any method, the constructor can have
arguments<A NAME="Index384"></A><A NAME="Index385"></A> to allow you to specify
<I>how</I> an object is created. The above example can easily be changed so the
constructor takes an argument:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:SimpleConstructor2.java</font>
<font color=#009900>// Constructors can have arguments.</font>

<font color=#0000ff>class</font> Rock2 {
  Rock2(<font color=#0000ff>int</font> i) {
    System.out.println(
      <font color=#004488>"Creating Rock number "</font> + i);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> SimpleConstructor2 {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; 10; i++)
      <font color=#0000ff>new</font> Rock2(i);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Constructor arguments provide you with a
way to provide parameters for the initialization of an object. For example, if
the class <B>Tree</B> has a constructor that takes a single integer argument
denoting the height of the tree, you would create a <B>Tree</B> object like
this: 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I7' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I8>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Tree t = <font color=#0000ff>new</font> Tree(12);  <font color=#009900>// 12-foot tree</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If <B>Tree(int)</B> is your only
constructor, then the compiler won&#8217;t let you create a <B>Tree</B> object
any other way.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I8' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I9>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Constructors eliminate a large class of
problems and make the code easier to read. In the preceding code fragment, for
example, you don&#8217;t see an explicit call to some <B>initialize(&#160;)</B>
method that is conceptually separate from definition. In Java, definition and
initialization are unified concepts&#8212;you can&#8217;t have one without the
other. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I9' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I10>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The constructor is an unusual type of
method because it has no return
value<A NAME="Index386"></A><A NAME="Index387"></A>. This is distinctly
different from a <B>void</B> return value, in which the method returns nothing
but you still have the option to make it return something else. Constructors
return nothing and you don&#8217;t have an option. If there was a return value,
and if you could select your own, the compiler would somehow need to know what
to do with that return value.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I10' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I11>
</FONT><A NAME="_Toc375545276"></A><A NAME="_Toc481064570"></A><BR></P></DIV>
<A NAME="Heading165"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Method overloading<BR><A NAME="Index388"></A><A NAME="Index389"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the important features in any
programming language is the use of names. When you create an object, you give a

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -