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

📄 tij308.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Chess <font color=#0000ff>extends</font> BoardGame {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  Chess() {
    <font color=#0000ff>super</font>(11);
    System.out.println(<font color=#004488>"Chess constructor"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Chess x = <font color=#0000ff>new</font> Chess();
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Game constructor"</font>,
      <font color=#004488>"BoardGame constructor"</font>,
      <font color=#004488>"Chess constructor"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>If you don&#146;t call the base-class constructor in <b>BoardGame(&#160;)</b>, the compiler will complain that it can&#146;t find a constructor of the form <b>Game(&#160;)</b>. In addition, the call to the base-class constructor <i>must</i> be the first thing you do in the derived-class constructor. (The compiler will remind you if you get it wrong.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_937" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading5291"></a>Catching base constructor exceptions<br></h4>
<p><a name="Index519"></a><a name="Index520"></a><a name="Index521"></a>As just noted, the compiler forces you to place the base-class constructor call first in the body of the derived-class constructor. This means nothing else can appear before it. As you&#146;ll see in Chapter 9, this also prevents a derived-class constructor from catching any exceptions that come from a base class. This can be inconvenient at times. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_938" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc305593254"></a><a name="_Toc305628726"></a><a name="_Toc312374019"></a><a name="_Toc375545309"></a><a name="_Toc24775638"></a><a name="Heading5293"></a>Combining
composition <br>and inheritance<br></h2>
<p><a name="Index522"></a><a name="Index523"></a>It is very common to use composition and inheritance together. The following example shows the creation of a more complex class, using both inheritance and composition, along with the necessary constructor initialization:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c06:PlaceSetting.java</font>
<font color=#009900>// Combining composition &amp; inheritance.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> Plate {
  Plate(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Plate constructor"</font>);
  }
}

<font color=#0000ff>class</font> DinnerPlate <font color=#0000ff>extends</font> Plate {
  DinnerPlate(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i);
    System.out.println(<font color=#004488>"DinnerPlate constructor"</font>);
  }
}

<font color=#0000ff>class</font> Utensil {
  Utensil(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Utensil constructor"</font>);
  }
}

<font color=#0000ff>class</font> Spoon <font color=#0000ff>extends</font> Utensil {
  Spoon(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i);
    System.out.println(<font color=#004488>"Spoon constructor"</font>);
  }
}

<font color=#0000ff>class</font> Fork <font color=#0000ff>extends</font> Utensil {
  Fork(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i);
    System.out.println(<font color=#004488>"Fork constructor"</font>);
  }
}

<font color=#0000ff>class</font> Knife <font color=#0000ff>extends</font> Utensil {
  Knife(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i);
    System.out.println(<font color=#004488>"Knife constructor"</font>);
  }
}

<font color=#009900>// A cultural way of doing something:</font>
<font color=#0000ff>class</font> Custom {
  Custom(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Custom constructor"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> PlaceSetting <font color=#0000ff>extends</font> Custom {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>private</font> Spoon sp;
  <font color=#0000ff>private</font> Fork frk;
  <font color=#0000ff>private</font> Knife kn;
  <font color=#0000ff>private</font> DinnerPlate pl;
  <font color=#0000ff>public</font> PlaceSetting(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i + 1);
    sp = <font color=#0000ff>new</font> Spoon(i + 2);
    frk = <font color=#0000ff>new</font> Fork(i + 3);
    kn = <font color=#0000ff>new</font> Knife(i + 4);
    pl = <font color=#0000ff>new</font> DinnerPlate(i + 5);
    System.out.println(<font color=#004488>"PlaceSetting constructor"</font>);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    PlaceSetting x = <font color=#0000ff>new</font> PlaceSetting(9);
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Custom constructor"</font>,
      <font color=#004488>"Utensil constructor"</font>,
      <font color=#004488>"Spoon constructor"</font>,
      <font color=#004488>"Utensil constructor"</font>,
      <font color=#004488>"Fork constructor"</font>,
      <font color=#004488>"Utensil constructor"</font>,
      <font color=#004488>"Knife constructor"</font>,
      <font color=#004488>"Plate constructor"</font>,
      <font color=#004488>"DinnerPlate constructor"</font>,
      <font color=#004488>"PlaceSetting constructor"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Although the compiler forces you to initialize the base classes, and requires that you do it right at the beginning of the constructor, it doesn&#146;t watch over you to make sure that you initialize the member objects, so you must remember to pay attention to that. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_939" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545310"></a><a name="_Toc24775639"></a><a name="Heading5378"></a>Guaranteeing
proper cleanup</h3>
<p>Java doesn&#146;t have the C++ concept of a <a name="Index526"></a><i>destructor</i>, a method that is automatically called when an object is destroyed. The reason is probably that in Java, the practice is simply to forget about objects rather than to destroy them, allowing the garbage collector to reclaim the memory as necessary. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_940" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index527"></a><a name="Index528"></a>Often this is fine, but there are times when your class might perform some activities during its lifetime that require cleanup. As mentioned in Chapter 4, you can&#146;t know when the garbage collector will be called, or if it will be called. So if you want something cleaned up for a class, you must explicitly write a special method to do it, and make sure that the client programmer knows that they must call this method. On top of this&#151;as described in Chapter 9 (&#147;Error Handling with Exceptions&#148;)&#151;you must guard against an exception by putting such cleanup in a <a name="Index529"></a><a name="Index530"></a><b>finally</b> clause. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_941" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Consider an example of a computer-aided design system that draws pictures on the screen:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c06:CADSystem.java</font>
<font color=#009900>// Ensuring proper cleanup.</font>
<font color=#0000ff>package</font> c06;
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>class</font> Shape {
  Shape(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Shape constructor"</font>);
  }
  <font color=#0000ff>void</font> dispose() {
    System.out.println(<font color=#004488>"Shape dispose"</font>);
  }
}

<font color=#0000ff>class</font> Circle <font color=#0000ff>extends</font> Shape {
  Circle(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i);
    System.out.println(<font color=#004488>"Drawing Circle"</font>);
  }
  <font color=#0000ff>void</font> dispose() {
    System.out.println(<font color=#004488>"Erasing Circle"</font>);
    <font color=#0000ff>super</font>.dispose();
  }
}

⌨️ 快捷键说明

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