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

📄 chapter06.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>class</font> Game {
  Game(<font color=#0000ff>int</font> i) {
    System.out.println(<font color=#004488>"Game constructor"</font>);
  }
}

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

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Chess <font color=#0000ff>extends</font> BoardGame {
  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();
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you don&#8217;t call the
base-class constructor in <B>BoardGame(&#160;)</B>, the compiler will complain
that it can&#8217;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><BR></P></DIV>
<A NAME="Heading184"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Catching base constructor
exceptions<BR><A NAME="Index456"></A><A NAME="Index457"></A><A NAME="Index458"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;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><A NAME="_Toc305593254"></A><A NAME="_Toc305628726"></A><A NAME="_Toc312374019"></A><A NAME="_Toc375545309"></A><A NAME="_Toc408018512"></A><BR></P></DIV>
<A NAME="Heading185"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Combining composition <BR>and
inheritance<BR><A NAME="Index459"></A><A NAME="Index460"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 <A NAME="Index461"></A><A NAME="Index462"></A>constructor
initialization:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: PlaceSetting.java</font>
<font color=#009900>// Combining composition &amp; inheritance</font>

<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 {
  Spoon sp;
  Fork frk;
  Knife kn;
  DinnerPlate pl;
  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);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While the compiler forces you to
initialize the base classes, and requires that you do it right at the beginning
of the constructor, it doesn&#8217;t watch over you to make sure that you
initialize the member objects, so you must remember to pay attention to
that.</FONT><A NAME="_Toc375545310"></A><A NAME="_Toc408018513"></A><BR></P></DIV>
<A NAME="Heading186"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Guaranteeing proper cleanup</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java doesn&#8217;t have the C++
concept of a <A NAME="Index463"></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 <A NAME="Index464"></A><A NAME="Index465"></A>garbage collector to
reclaim the memory as necessary.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;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 write a special method to do it
explicitly, and make sure that the client programmer knows that they must call
this method. On top of this, as described in Chapter 9
(<A NAME="Index466"></A>exception handling), you must guard against an exception
by putting such cleanup in a <A NAME="Index467"></A><B>finally</B>
clause.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consider an example of a
computer-aided design system that draws pictures on the screen:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: CADSystem.java</font>
<font color=#009900>// Ensuring proper cleanup</font>
<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> cleanup() {
    System.out.println(<font color=#004488>"Shape cleanup"</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 a Circle"</font>);
  }
  <font color=#0000ff>void</font> cleanup() {
    System.out.println(<font color=#004488>"Erasing a Circle"</font>);
    <font color=#0000ff>super</font>.cleanup();
  }
}

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

<font color=#0000ff>class</font> Line <font color=#0000ff>extends</font> Shape {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> start, end;
  Line(<font color=#0000ff>int</font> start, <font color=#0000ff>int</font> end) {
    <font color=#0000ff>super</font>(start);
    <font color=#0000ff>this</font>.start = start;
    <font color=#0000ff>this</font>.end = end;
    System.out.println(<font color=#004488>"Drawing a Line: "</font> +
           start + <font color=#004488>", "</font> + end);
  }
  <font color=#0000ff>void</font> cleanup() {
    System.out.println(<font color=#004488>"Erasing a Line: "</font> +
           start + <font color=#004488>", "</font> + end);
    <font color=#0000ff>super</font>.cleanup();
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> CADSystem <font color=#0000ff>extends</font> Shape {
  <font color=#0000ff>private</font> Circle c;
  <font color=#0000ff>private</font> Triangle t;
  <font color=#0000ff>private</font> Line[] lines = <font color=#0000ff>new</font> Line[10];
  CADSystem(<font color=#0000ff>int</font> i) {
    <font color=#0000ff>super</font>(i + 1);
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> j = 0; j &lt; 10; j++)
      lines[j] = <font color=#0000ff>new</font> Line(j, j*j);
    c = <font color=#0000ff>new</font> Circle(1);
    t = <font color=#0000ff>new</font> Triangle(1);
    System.out.println(<font color=#004488>"Combined constructor"</font>);
  }
  <font color=#0000ff>void</font> cleanup() {
    System.out.println(<font color=#004488>"CADSystem.cleanup()"</font>);
    t.cleanup();
    c.cleanup();
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; lines.length; i++)
      lines[i].cleanup();
    <font color=#0000ff>super</font>.cleanup();
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    CADSystem x = <font color=#0000ff>new</font> CADSystem(47);
    <font color=#0000ff>try</font> {
      <font color=#009900>// Code and exception handling...</font>
    } <font color=#0000ff>finally</font> {
      x.cleanup();
    }
  }

⌨️ 快捷键说明

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