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

📄 tij306.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>class</font> Tree {
  <font color=#0000ff>int</font> height;
  Tree() {
    System.out.println(<font color=#004488>"Planting a seedling"</font>);
    height = 0;
  }
  Tree(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Creating new Tree that is "</font>
      + i + <font color=#004488>" feet tall"</font>);
    height = i;
  }
  <font color=#0000ff>void</font> info() {
    System.out.println(<font color=#004488>"Tree is "</font> + height + <font color=#004488>" feet tall"</font>);
  }
  <font color=#0000ff>void</font> info(String s) {
    System.out.println(s + <font color=#004488>": Tree is "</font>
      + height + <font color=#004488>" feet tall"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Overloading {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <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();
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Creating new Tree that is 0 feet tall"</font>,
      <font color=#004488>"Tree is 0 feet tall"</font>,
      <font color=#004488>"overloaded method: Tree is 0 feet tall"</font>,
      <font color=#004488>"Creating new Tree that is 1 feet tall"</font>,
      <font color=#004488>"Tree is 1 feet tall"</font>,
      <font color=#004488>"overloaded method: Tree is 1 feet tall"</font>,
      <font color=#004488>"Creating new Tree that is 2 feet tall"</font>,
      <font color=#004488>"Tree is 2 feet tall"</font>,
      <font color=#004488>"overloaded method: Tree is 2 feet tall"</font>,
      <font color=#004488>"Creating new Tree that is 3 feet tall"</font>,
      <font color=#004488>"Tree is 3 feet tall"</font>,
      <font color=#004488>"overloaded method: Tree is 3 feet tall"</font>,
      <font color=#004488>"Creating new Tree that is 4 feet tall"</font>,
      <font color=#004488>"Tree is 4 feet tall"</font>,
      <font color=#004488>"overloaded method: Tree is 4 feet tall"</font>,
      <font color=#004488>"Planting a seedling"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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 is a default constructor, and one that takes the existing height. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_663" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You might also want to call the <b>info(&#160;)</b> method in more than one way. For example, if you have an extra message you want printed, you can use <b>info(String)</b>, and <b>info(&#160;)</b> 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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_664" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545277"></a><a name="_Toc24775602"></a><a name="Heading3491"></a>Distinguishing
overloaded methods<br></h3>
<p><a name="Index338"></a><a name="Index339"></a>If the methods have the same name, how can Java know which method you mean? There&#146;s a simple rule: each overloaded method must take a unique list of argument types. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_665" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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? <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_666" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Even differences in the ordering of arguments are sufficient to distinguish two methods: (Although you don&#146;t normally want to take this approach, as it produces difficult-to-maintain code.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_667" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:OverloadingOrder.java</font>
<font color=#009900>// Overloading based on the order of the arguments.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> OverloadingOrder {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <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>);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"String: String first, int: 11"</font>,
      <font color=#004488>"int: 99, String: Int first"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The two <b>print(&#160;)</b> methods have identical arguments, but the order is different, and that&#146;s what makes them distinct. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_668" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc312373866"></a><a name="_Toc375545278"></a><a name="_Toc24775603"></a><a name="Heading3518"></a>Overloading
with primitives</h3>
<p>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:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:PrimitiveOverloading.java</font>
<font color=#009900>// Promotion of primitives and overloading.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> PrimitiveOverloading {
  <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>void</font> f1(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f1(char)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>byte</font> x) { System.out.println(<font color=#004488>"f1(byte)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>short</font> x) { System.out.println(<font color=#004488>"f1(short)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>int</font> x) { System.out.println(<font color=#004488>"f1(int)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>long</font> x) { System.out.println(<font color=#004488>"f1(long)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>float</font> x) { System.out.println(<font color=#004488>"f1(float)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>double</font> x) { System.out.println(<font color=#004488>"f1(double)"</font>); }

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

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

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

  <font color=#0000ff>void</font> f5(<font color=#0000ff>long</font> x) { System.out.println(<font color=#004488>"f5(long)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>float</font> x) { System.out.println(<font color=#004488>"f5(float)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>double</font> x) { System.out.println(<font color=#004488>"f5(double)"</font>); }

⌨️ 快捷键说明

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