📄 tij0066.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0065.html">Prev</a> | <a href="tij0067.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Combining
composition
<P>and
inheritance
<P><A NAME="Index459"></A><A NAME="Index460"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: PlaceSetting.java</font>
<font color="#009900">// Combining composition & inheritance</font>
<font color="#0000ff">class</font> Plate {
Plate(<font color="#0000ff">int</font> i) {
System.out.println("Plate constructor");
}
}
<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(
"DinnerPlate constructor");
}
}
<font color="#0000ff">class</font> Utensil {
Utensil(<font color="#0000ff">int</font> i) {
System.out.println("Utensil constructor");
}
}
<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("Spoon constructor");
}
}
<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("Fork constructor");
}
}
<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("Knife constructor");
}
}
<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("Custom constructor");
}
}
<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(
"PlaceSetting constructor");
}
<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">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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’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><P></DIV>
<A NAME="Heading187"></A><H3 ALIGN=LEFT>
Guaranteeing
proper cleanup
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
doesn’t have the C++ concept of a <A NAME="Index463"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>destructor</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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’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></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
clause.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Consider
an example of a computer-aided design system that draws pictures on the screen:
</FONT><P></DIV>
<font color="#990000"><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("Shape constructor");
}
<font color="#0000ff">void</font> cleanup() {
System.out.println("Shape cleanup");
}
}
<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("Drawing a Circle");
}
<font color="#0000ff">void</font> cleanup() {
System.out.println("Erasing a Circle");
<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("Drawing a Triangle");
}
<font color="#0000ff">void</font> cleanup() {
System.out.println("Erasing a Triangle");
<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("Drawing a Line: " +
start + ", " + end);
}
<font color="#0000ff">void</font> cleanup() {
System.out.println("Erasing a Line: " +
start + ", " + end);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -