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

📄 chap04.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
name to a region of storage. A method is a name for an action. By using names to
describe your system, you create a program that is easier for people to
understand and change. It’s a lot like writing prose—the goal is to
communicate with your readers.

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

<backtalk:display ID=TIJ3_CHAPTER4_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You refer to all objects and methods by
using names. Well-chosen names make it easier for you and others to understand
your code.  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I12' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A problem arises when mapping the concept
of nuance in human language onto a programming language. Often, the same word
expresses a number of different meanings&#8212;it&#8217;s <I>overloaded</I>.
This is useful, especially when it comes to trivial differences. You say
&#8220;wash the shirt,&#8221; &#8220;wash the car,&#8221; and &#8220;wash the
dog.&#8221; It would be silly to be forced to say, &#8220;shirtWash the
shirt,&#8221; &#8220;carWash the car,&#8221; and &#8220;dogWash the dog&#8221;
just so the listener doesn&#8217;t need to make any distinction about the action
performed. Most human languages are redundant, so even if you miss a few words,
you can still determine the meaning. We don&#8217;t need unique
identifiers&#8212;we can deduce meaning from context.

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

<backtalk:display ID=TIJ3_CHAPTER4_I14>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most programming languages (C in
particular) require you to have a unique identifier for each function. So you
could not have one function called <B>print(&#160;)</B> for printing integers
and another called <B>print(&#160;)</B> for printing floats&#8212;each function
requires a unique name. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I14' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER4_I15>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java (and C++), another factor forces
the overloading of method names: the
constructor<A NAME="Index390"></A><A NAME="Index391"></A>. Because the
constructor&#8217;s name is predetermined by the name of the class, there can be
only one constructor name. But what if you want to create an object in more than
one way? For example, suppose you build a class that can initialize itself in a
standard way or by reading information from a file. You need two constructors,
one that takes no arguments (the <I>default</I> constructor, also called the
<I>no-arg</I> constructor), and one that takes a <B>String</B> as an argument,
which is the name of the file from which to initialize the object. Both are
constructors, so they must have the same name&#8212;the name of the class. Thus,
<I>method overloading</I> is essential to allow the same method name to be used
with different argument types. And although method overloading is a must for
constructors, it&#8217;s a general convenience and can be used with any method. 

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

<backtalk:display ID=TIJ3_CHAPTER4_I16>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s an example that shows both
overloaded constructors and overloaded ordinary methods:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:Overloading.java</font>
<font color=#009900>// Demonstration of both constructor</font>
<font color=#009900>// and ordinary method overloading.</font>
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>class</font> Tree {
  <font color=#0000ff>int</font> height;
  Tree() {
    prt(<font color=#004488>"Planting a seedling"</font>);
    height = 0;
  }
  Tree(<font color=#0000ff>int</font> i) {
    prt(<font color=#004488>"Creating new Tree that is "</font>
        + i + <font color=#004488>" feet tall"</font>);
    height = i;
  }
  <font color=#0000ff>void</font> info() {
    prt(<font color=#004488>"Tree is "</font> + height
        + <font color=#004488>" feet tall"</font>);
  }
  <font color=#0000ff>void</font> info(String s) {
    prt(s + <font color=#004488>": Tree is "</font>
        + height + <font color=#004488>" feet tall"</font>);
  }
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) {
    System.out.println(s);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Overloading {
  <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; 5; i++) {
      Tree t = <font color=#0000ff>new</font> Tree(i);
      t.info();
      t.info(<font color=#004488>"overloaded method"</font>);
    }
    <font color=#009900>// Overloaded constructor:</font>
    <font color=#0000ff>new</font> Tree();
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>Tree</B> object can be created
either as a seedling, with no argument, or as a plant grown in a nursery, with
an existing height. To support this, there are two constructors, one that takes
no arguments (we call constructors that take no arguments
<A NAME="Index392"></A><A NAME="Index393"></A><I>default
constructors</I></FONT><A NAME="fnB27" HREF="#fn27">[27]</A><A NAME="Index394"></A><A NAME="Index395"></A><FONT FACE="Georgia">)
and one that takes the existing height.

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

<backtalk:display ID=TIJ3_CHAPTER4_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might also want to call the
<B>info(&#160;)</B> method in more than one way. For example, with a
<B>String</B> argument if you have an extra message you want printed, and
without if you have nothing more to say. It would seem strange to give two
separate names to what is obviously the same concept. Fortunately, method
overloading allows you to use the same name for both.

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

<backtalk:display ID=TIJ3_CHAPTER4_I18>
</FONT><A NAME="_Toc375545277"></A><A NAME="_Toc481064571"></A><BR></P></DIV>
<A NAME="Heading166"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Distinguishing overloaded
methods<BR><A NAME="Index396"></A><A NAME="Index397"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If the methods have the same name, how
can Java know which method you mean? There&#8217;s a simple rule: each
overloaded method must take a unique list of argument types.

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

<backtalk:display ID=TIJ3_CHAPTER4_I19>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you think about this for a second, it
makes sense: how else could a programmer tell the difference between two methods
that have the same name, other than by the types of their arguments?

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

<backtalk:display ID=TIJ3_CHAPTER4_I20>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Even differences in the ordering of
arguments are sufficient to distinguish two methods: (Although you don&#8217;t
normally want to take this approach, as it produces difficult-to-maintain code.)

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

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

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:OverloadingOrder.java</font>
<font color=#009900>// Overloading based on the order of</font>
<font color=#009900>// the arguments.</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> OverloadingOrder {
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(String s, <font color=#0000ff>int</font> i) {
    System.out.println(
      <font color=#004488>"String: "</font> + s +
      <font color=#004488>", int: "</font> + i);
  }
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(<font color=#0000ff>int</font> i, String s) {
    System.out.println(
      <font color=#004488>"int: "</font> + i +
      <font color=#004488>", String: "</font> + s);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    print(<font color=#004488>"String first"</font>, 11);
    print(99, <font color=#004488>"Int first"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The two <B>print(&#160;)</B> methods have
identical arguments, but the order is different, and that&#8217;s what makes
them distinct.

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

<backtalk:display ID=TIJ3_CHAPTER4_I22>
</FONT><A NAME="_Toc312373866"></A><A NAME="_Toc375545278"></A><A NAME="_Toc481064572"></A><BR></P></DIV>
<A NAME="Heading167"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Overloading with primitives</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A primitive can be automatically promoted
from a smaller type to a larger one and this can be slightly confusing in
combination with overloading. The following example demonstrates what happens
when a primitive is handed to an overloaded method:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:PrimitiveOverloading.java</font>
<font color=#009900>// Promotion of primitives and overloading.</font>

<font color=#0000ff>public</font> <font color=#0000ff>class</font> PrimitiveOverloading {
  <font color=#009900>// boolean can't be automatically converted</font>
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) { 
    System.out.println(s); 
  }

  <font color=#0000ff>void</font> f1(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f1(char)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f1(byte)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f1(short)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f1(int)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f1(long)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f1(float)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f1(double)"</font>); }

  <font color=#0000ff>void</font> f2(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f2(byte)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f2(short)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f2(int)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f2(long)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f2(float)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f2(double)"</font>); }

  <font color=#0000ff>void</font> f3(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f3(short)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f3(int)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f3(long)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f3(float)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f3(double)"</font>); }

  <font color=#0000ff>void</font> f4(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f4(int)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f4(long)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f4(float)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f4(double)"</font>); }

  <font color=#0000ff>void</font> f5(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f5(long)"</font>); }

⌨️ 快捷键说明

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