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

📄 tij306.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You&#146;ll see that the constant value 5 is treated as an <b>int</b>, so if an overloaded method is available that takes an <b>int</b>, it is used. In all other cases, if you have a data type that is smaller than the argument in the method, that data type is promoted. <b>char</b> produces a slightly different effect, since if it doesn&#146;t find an exact <b>char</b> match, it is promoted to <b>int</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_669" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>What happens if your argument is <i>bigger</i> than the argument expected by the overloaded method? A modification of the preceding program gives the answer:<br></p>

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

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Demotion {
  <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>char</font> x) { System.out.println(<font color=#004488>"f2(char)"</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> f3(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f3(char)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>byte</font> x) { System.out.println(<font color=#004488>"f3(byte)"</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> f4(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f4(char)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>byte</font> x) { System.out.println(<font color=#004488>"f4(byte)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>short</font> x) { System.out.println(<font color=#004488>"f4(short)"</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> f5(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f5(char)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>byte</font> x) { System.out.println(<font color=#004488>"f5(byte)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>short</font> x) { System.out.println(<font color=#004488>"f5(short)"</font>); }

  <font color=#0000ff>void</font> f6(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f6(char)"</font>); }
  <font color=#0000ff>void</font> f6(<font color=#0000ff>byte</font> x) { System.out.println(<font color=#004488>"f6(byte)"</font>); }

  <font color=#0000ff>void</font> f7(<font color=#0000ff>char</font> x) { System.out.println(<font color=#004488>"f7(char)"</font>); }

  <font color=#0000ff>void</font> testDouble() {
    <font color=#0000ff>double</font> x = 0;
    System.out.println(<font color=#004488>"double argument:"</font>);
    f1(x);f2((<font color=#0000ff>float</font>)x);f3((<font color=#0000ff>long</font>)x);f4((<font color=#0000ff>int</font>)x);
    f5((<font color=#0000ff>short</font>)x);f6((<font color=#0000ff>byte</font>)x);f7((<font color=#0000ff>char</font>)x);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Demotion p = <font color=#0000ff>new</font> Demotion();
    p.testDouble();
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"double argument:"</font>,
      <font color=#004488>"f1(double)"</font>,
      <font color=#004488>"f2(float)"</font>,
      <font color=#004488>"f3(long)"</font>,
      <font color=#004488>"f4(int)"</font>,
      <font color=#004488>"f5(short)"</font>,
      <font color=#004488>"f6(byte)"</font>,
      <font color=#004488>"f7(char)"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Here, the methods take narrower primitive values. If your argument is wider, then you must <a name="Index340"></a><i>cast</i> to the necessary type by placing the type name inside parentheses. If you don&#146;t do this, the compiler will issue an error message. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_670" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You should be aware that this is a <a name="Index341"></a><a name="Index342"></a><i>narrowing conversion,</i> which means you might lose information during the cast. This is why the compiler forces you to do it&#151;to flag the narrowing conversion. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_671" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775604"></a><a name="Heading3747"></a>Overloading on return
values<br></h3>
<p><a name="Index343"></a><a name="Index344"></a>It is common to wonder &#147;Why only class names and method argument lists? Why not distinguish between methods based on their return values?&#148; For example, these two methods, which have the same name and arguments, are easily distinguished from each other: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_672" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>void</font> f() {}
<font color=#0000ff>int</font> f() {}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This works fine when the compiler can unequivocally determine the meaning from the context, as in <b>int x = f(&#160;)</b>. However, you can also call a method and ignore the return value. This is often referred to as <i>calling a method for its </i><a name="Index345"></a><i>side effect</i>, since you don&#146;t care about the return value, but instead want the other effects of the method call. So if you call the method this way: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_673" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>f();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>how can Java determine which <b>f(&#160;)</b> should be called? And how could someone reading the code see it? Because of this sort of problem, you cannot use return value types to distinguish overloaded methods. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_674" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545279"></a><a name="_Toc24775605"></a><a name="Heading3756"></a>Default
constructors</h3>
<p>As mentioned previously, a default constructor (a.k.a. a &#147;no-arg&#148; constructor) is one without arguments that is used to create a &#147;basic object.&#148; If you create a class that has no constructors, the compiler will automatically create a default constructor for you. For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_675" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:DefaultConstructor.java</font>

<font color=#0000ff>class</font> Bird {
  <font color=#0000ff>int</font> i;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> DefaultConstructor {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Bird nc = <font color=#0000ff>new</font> Bird(); <font color=#009900>// Default!</font>
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The line <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_676" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>new</font> Bird();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>creates a new object and calls the default constructor, even though one was not explicitly defined. Without it, we would have no method to call to build our object. However, if you define any constructors (with or without arguments), the compiler will <i>not</i> synthesize one for you: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_677" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Hat {
  Hat(<font color=#0000ff>int</font> i) {}
  Hat(<font color=#0000ff>double</font> d) {}
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Now if you say: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_678" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>new</font> Hat();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>the compiler will complain that it cannot find a constructor that matches. It&#146;s as if when you don&#146;t put in any constructors, the compiler says &#147;You are bound to need <i>some</i> constructor, so let me make one for you.&#148; But if you write a constructor, the compiler says &#147;You&#146;ve written a constructor so you know what you&#146;re doing; if you didn&#146;t put in a default it&#146;s because you meant to leave it out.&#148; <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_679" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545280"></a><a name="_Toc24775606"></a><a name="Heading3783"></a>The
<a name="Index348"></a><b>this </b>keyword</h3>
<p>If you have two objects of the same type called <b>a</b> and <b>b</b>, you might wonder how it is that you can call a method <b>f(&#160;)</b> for both those objects: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_680" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Banana { <font color=#0000ff>void</font> f(<font color=#0000ff>int</font> i) { <font color=#009900>/* ... */</font> } }
Banana a = <font color=#0000ff>new</font> Banana(), b = <font color=#0000ff>new</font> Banana();
a.f(1);
b.f(2);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>If there&#146;s only one method called <b>f(&#160;)</b>, how can that method know whether it&#146;s being called for the object <b>a</b> or <b>b</b>? <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_681" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>To allow you to write the code in a convenient object-oriented syntax in which you &#147;send a message to an object,&#148; the compiler does some undercover work for you. There&#146;s a secret first argument passed to the method <b>f(&#160;)</b>, and that argument is the reference to the object that&#146;s being manipulated. So the two method calls become something like: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap04_682" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Banana.f(a,1);
Banana.f(b,2);</PRE></FONT></BLOCKQUOTE><p><br></p>

⌨️ 快捷键说明

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