📄 tij0063.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="tij0062.html">Prev</a> | <a href="tij0064.html">Next</a>
</td>
</tr></table>
<hr>
<H1 ALIGN=LEFT>
6:
Reusing classes
</H1>
<DIV ALIGN=LEFT><FONT FACE="Calligraph421 BT" SIZE=4 COLOR="Black">One
of the most compelling features about Java is code reuse<A NAME="Index423"></A><A NAME="Index424"></A>.
But to be revolutionary, you’ve got to be able to do a lot more than copy
code and change it.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">That’s
the approach used in procedural languages like C, and it hasn’t worked
very well. Like everything in Java, the solution revolves around the class. You
reuse code by creating new classes, but instead of creating them from scratch,
you use existing classes that someone has already built and debugged.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
trick is to use the classes without soiling the existing code. In this chapter
you’ll see two ways to accomplish this. The first is quite
straightforward: You simply create objects of your existing class inside the
new class. This is called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>composition</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
<A NAME="Index425"></A>because
the new class is composed of objects of existing classes. You’re simply
reusing the functionality of the code, not its form.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second approach is more subtle. It creates a new class as a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>type
of
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
an existing class. You literally take the form of the existing class and add
code to it without modifying the existing class. This magical act is called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>inheritance<A NAME="Index426"></A></I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and the compiler does most of the work. Inheritance is one of the cornerstones
of object-oriented programming and has additional implications that will be
explored in the next chapter.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
turns out that much of the syntax and behavior are similar for both composition
and inheritance (which makes sense because they are both ways of making new
types from existing types). In this chapter, you’ll learn about these
code reuse mechanisms.
</FONT><a name="_Toc305593251"></a><a name="_Toc305628723"></a><a name="_Toc312374014"></a><a name="_Toc375545306"></a><a name="_Toc408018509"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0062.html">Prev</a> | <a href="tij0064.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -