正在加载图片...
Thinking in Java 3 Edition public static void main(string[ args)t / Upcasting during addition to the array new Percussion(), new Stringed() new Woodwind( monitor. expect (new String[] t Wind. play() Middle C", ercussion. play () Middle C Stringed play() Middle c Brass. play ( Middle C 其它代码的工作方式没变。不论是把它上传到一个叫 Instrument的 普通”类,还是一个叫 Instrument的 abstract类,还是一个叫 Instrument的 interface,它的工作方式都是一样的。实际上,你根 本没法从tune()来判断,工 nstrument到底是“普通”类,还是 abstract类,或 interface。这就是它的本意:它让程序员自己去选择 要在那个级别上控制对象的创建和使用。 Java的“多重继承” interface不仅仅是一种“更纯”的 abstract类。它还有更高一层的 目的。由于 interface不带任何“实现”—也就是说 interface和内 存无关—因此不会有谁去组绕 interface之间的结合。这一点非常重 要,因为有时你会遇到“x既是a又是b,而且还是c”的情况。在 C++中,这种“将多个类的接口结合在一起”的行为被称作“多重继承 ( multiple inheritance)”,但是由于每个类又都有它自己的实现,而这 会带来很多“甩都甩不掉”的问题。Java能让你作同样的事情,但是这 时只有一个类可以有实现,因此当你合并Java接口的时候,就不会有这 种问题了 第5页共47页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.comThinking in Java 3 rd Edition ✄ 5 ☎ ✆ 47 ☎ www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com public static void main(String[] args) { // Upcasting during addition to the array: Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() }; tuneAll(orchestra); monitor.expect(new String[] { "Wind.play() Middle C", "Percussion.play() Middle C", "Stringed.play() Middle C", "Brass.play() Middle C", "Woodwind.play() Middle C" }); } } ///:~ §AK€(y1µ´œX PAJAg3ס In strumen t ( ͕Ï 3ס In strumen t ( abstract  3ס In strumen t ( in terface A(y1µ´Æ3¦(ñ¢J ¡; œ tu n e( )Q£¤ In strumen t gh͕Ï  abstract  3 in terface A( ¹LAÇâ%&ýþ¥¦ %FYק¨J¥!(12çˆ Java  in terface X<<3†ÍbÏ( abstract Am©3ª( «(7) in terface X¬tÍñíÏ55[$ in terface  €Ü55æòXºm­® in terface u4(¯° 3C¯· % æ™m¡º±gÍx ² a ü b à³ cÏ(´F C+ + * †ÍV×(¯°F39Ï(}™vµ1ÍVö8 (multiple inheritance)Ï 7)¶×üÆmAýþ(ñí à º¬QŠVÍ·Æ·X¸Ï(š¹Java ;Ç¡1‡¦(´  óm3×ÝEmñí æò ¡°ž Java (M Xºm †š¹,L
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有