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

📄 tij308.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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 Triangle"</font>);
  }
  <font color=#0000ff>void</font> dispose() {
    System.out.println(<font color=#004488>"Erasing Triangle"</font>);
    <font color=#0000ff>super</font>.dispose();
  }
}

<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 Line: "</font>+ start+ <font color=#004488>", "</font>+ end);
  }
  <font color=#0000ff>void</font> dispose() {
    System.out.println(<font color=#004488>"Erasing Line: "</font>+ start+ <font color=#004488>", "</font>+ end);
    <font color=#0000ff>super</font>.dispose();
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> CADSystem <font color=#0000ff>extends</font> Shape {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <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[5];
  <font color=#0000ff>public</font> 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; lines.length; 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>public</font> <font color=#0000ff>void</font> dispose() {
    System.out.println(<font color=#004488>"CADSystem.dispose()"</font>);
    <font color=#009900>// The order of cleanup is the reverse</font>
    <font color=#009900>// of the order of initialization</font>
    t.dispose();
    c.dispose();
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = lines.length - 1; i &gt;= 0; i--)
      lines[i].dispose();
    <font color=#0000ff>super</font>.dispose();
  }
  <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.dispose();
    }
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Line: 0, 0"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Line: 1, 1"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Line: 2, 4"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Line: 3, 9"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Line: 4, 16"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Circle"</font>,
      <font color=#004488>"Shape constructor"</font>,
      <font color=#004488>"Drawing Triangle"</font>,
      <font color=#004488>"Combined constructor"</font>,
      <font color=#004488>"CADSystem.dispose()"</font>,
      <font color=#004488>"Erasing Triangle"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Circle"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Line: 4, 16"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Line: 3, 9"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Line: 2, 4"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Line: 1, 1"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Erasing Line: 0, 0"</font>,
      <font color=#004488>"Shape dispose"</font>,
      <font color=#004488>"Shape dispose"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Everything in this system is some kind of <b>Shape</b> (which is itself a kind of <b>Object</b>, since it&#146;s implicitly inherited from the root class). Each class overrides <b>Shape</b>&#146;s <b>dispose(&#160;)</b> method in addition to calling the base-class version of that method using <b>super</b>. The specific <b>Shape</b> classes&#151;<b>Circle</b>, <b>Triangle</b>, and <b>Line</b>&#151;all have constructors that &#147;draw,&#148; although any method called during the lifetime of the object could be responsible for doing something that needs cleanup. Each class has its own <b>dispose(&#160;)</b> method to restore nonmemory things back to the way they were before the object existed. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_942" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In <b>main(&#160;)</b>, you can see two keywords that are new, and won&#146;t officially be introduced until Chapter 9: <a name="Index531"></a><b>try</b> and <a name="Index532"></a><b>finally</b>. The <b>try</b> keyword indicates that the block that follows (delimited by curly braces) is a <i>guarded region</i>, which means that it is given special treatment. One of these special treatments is that the code in the <b>finally</b> clause following this guarded region is <i>always</i> executed, no matter how the <b>try</b> block exits. (With exception handling, it&#146;s possible to leave a <b>try</b> block in a number of nonordinary ways.) Here, the <b>finally</b> clause is saying &#147;always call <b>dispose(&#160;)</b> for <b>x</b>, no matter what happens.&#148; These keywords will be explained thoroughly in Chapter 9. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_943" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Note that in your cleanup method, you must also pay attention to the calling order for the base-class and member-object cleanup methods in case one subobject depends on another. In general, you should follow the same form that is imposed by a C++ compiler on its destructors: first perform all of the cleanup work specific to your class, in the reverse order of creation. (In general, this requires that base-class elements still be viable.) Then call the base-class cleanup method, as demonstrated here. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_944" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>There can be many cases in which the cleanup issue is not a problem; you just let the garbage collector do the work. But when you must do it explicitly, diligence and attention are required, because there&#146;s not much you can rely on when it comes to garbage collection. The garbage collector might never be called. If it is, it can reclaim objects in any order it wants. It&#146;s best to not rely on garbage collection for anything but memory reclamation. If you want cleanup to take place, make your own cleanup methods and don&#146;t rely on <a name="Index533"></a><a name="Index534"></a><b>finalize(&#160;)</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap06_946" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc312374021"></a><a name="_Toc375545311"></a><a name="_Toc24775640"></a><a name="Heading5504"></a>Name
hiding<br></h3>
<p><a name="Index535"></a><a name="Index536"></a><a name="Index537"></a><a name="Index538"></a>If a Java base class has a method name that&#146;s overloaded several times, redefining that method name in the derived class will <i>not </i>hide any of the base-class versions (unlike C++). Thus overloading works regardless of whether the method was defined at this level or in a base class:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c06:Hide.java</font>
<font color=#009900>// Overloading a base-class method name in a derived class</font>
<font color=#009900>// does not hide the base-class versions.</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;

<font color=#0000ff>class</font> Homer {
  <font color=#0000ff>char</font> doh(<font color=#0000ff>char</font> c) {
    System.out.println(<font color=#004488>"doh(char)"</font>);
    <font color=#0000ff>return</font> 'd';
  }
  <font color=#0000ff>float</font> doh(<font color=#0000ff>float</font> f) {
    System.out.println(<font color=#004488>"doh(float)"</font>);
    <font color=#0000ff>return</font> 1.0f;
  }
}

<font color=#0000ff>class</font> Milhouse {}

<font color=#0000ff>class</font> Bart <font color=#0000ff>extends</font> Homer {
  <font color=#0000ff>void</font> doh(Milhouse m) {
    System.out.println(<font color=#004488>"doh(Milhouse)"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Hide {
  <font color=#0000ff>private</font> <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) {
    Bart b = <font color=#0000ff>new</font> Bart();
    b.doh(1);
    b.doh('x');
    b.doh(1.0f);
    b.doh(<font color=#0000ff>new</font> Milhouse());
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"doh(float)"</font>,
      <font color=#004488>"doh(char)"</font>,
      <font color=#004488>"doh(float)"</font>,
      <font color=#004488>"doh(Milhouse)"</font>
    });
  }

⌨️ 快捷键说明

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