亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? chap07.htm

?? Thinking in Java, 2nd edition
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:09
Translation Platform:Win32
Number of Output files:23
This File:Chap07.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>7: Polymorphism</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</a> ]
    [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
    [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/Services">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chap06.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="Chap08.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_7"></A><A NAME="_Toc375545326"></A><A NAME="_Toc477690727"></A><A NAME="_Toc481064624"></A><A NAME="Heading234"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
7: Polymorphism<A NAME="A"></A></H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER7_I0>
<A NAME="Index666"></A>Polymorphism
is the third essential feature of an object-oriented programming language, after
data abstraction and inheritance. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It provides another dimension of
separation of interface from implementation, to decouple <I>what</I> from
<I>how</I>. Polymorphism allows improved code organization and readability as
well as the creation of <I>extensible</I> programs that can be
&#8220;grown&#8221; not only during the original creation of the project but
also when new features are desired.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER7_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER7_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Encapsulation creates new data types by
combining characteristics and behaviors. Implementation hiding separates the
interface from the implementation by making the details <B>private</B>. This
sort of mechanical organization makes ready sense to someone with a procedural
programming background. But polymorphism deals with
<A NAME="Index667"></A>decoupling in terms of <I>types</I>. In the last chapter,
you saw how <A NAME="Index668"></A>inheritance allows the treatment of an object
as its own type <I>or</I> its base type. This ability is critical because it
allows many types (derived from the same base type) to be treated as if they
were one type, and a single piece of code to work on all those different types
equally. The <A NAME="Index669"></A>polymorphic method call allows one type to
express its distinction from another, similar type, as long as they&#8217;re
both derived from the same base type. This distinction is expressed through
differences in behavior of the methods that you can call through the base class.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER7_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER7_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this chapter, you&#8217;ll learn about
polymorphism (also called
<A NAME="Index670"></A><A NAME="Index671"></A><A NAME="Index672"></A><A NAME="Index673"></A><I>dynamic
binding</I> or <I>late binding</I> or <I>run-time binding</I>)<I> </I>starting
from the basics, with simple examples that strip away everything but the
polymorphic behavior of the program.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER7_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER7_I3>
</FONT><A NAME="_Toc305593263"></A><A NAME="_Toc305628735"></A><A NAME="_Toc312374040"></A><A NAME="_Toc375545327"></A><A NAME="_Toc481064625"></A><BR></P></DIV>
<A NAME="Heading235"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Upcasting revisited</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Chapter 6 you saw how an object can be
used as its own type or as an object of its base type. Taking an object
reference and treating it as a reference to its base type is called
<I>upcasting,<A NAME="Index674"></A></I> because of the way inheritance trees
are drawn with the base class at the top.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER7_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER7_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You also saw a problem arise, which is
embodied in the following:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c07:music:Music.java </font>
<font color=#009900>// Inheritance &amp; upcasting.</font>

<font color=#0000ff>class</font> Note {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> value;
  <font color=#0000ff>private</font> Note(<font color=#0000ff>int</font> val) { value = val; }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> Note
    MIDDLE_C = <font color=#0000ff>new</font> Note(0), 
    C_SHARP  = <font color=#0000ff>new</font> Note(1),
    B_FLAT   = <font color=#0000ff>new</font> Note(2);
} <font color=#009900>// Etc.</font>

<font color=#0000ff>class</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Instrument.play()"</font>);
  }
}

<font color=#009900>// Wind objects are instruments</font>
<font color=#009900>// because they have the same interface:</font>
<font color=#0000ff>class</font> Wind <font color=#0000ff>extends</font> Instrument {
  <font color=#009900>// Redefine interface method:</font>
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Wind.play()"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Music {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> tune(Instrument i) {
    <font color=#009900>// ...</font>
    i.play(Note.MIDDLE_C);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Wind flute = <font color=#0000ff>new</font> Wind();
    tune(flute); <font color=#009900>// Upcasting</font>
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The method <B>Music.tune(&#160;)</B>
accepts an <B>Instrument </B>reference, but also anything derived from
<B>Instrument</B>. In <B>main(&#160;)</B>, you can see this happening as a
<B>Wind</B> reference is passed to <B>tune(&#160;)</B>, with no cast necessary.
This is acceptable; the interface in <B>Instrument</B> must exist in
<B>Wind</B>, because <B>Wind</B> is inherited from <B>Instrument</B>. Upcasting
from <B>Wind</B> to <B>Instrument</B> may &#8220;narrow&#8221; that interface,
but it cannot make it anything less than the full interface to
<B>Instrument</B>.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER7_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER7_I5>
</FONT><A NAME="_Toc481064626"></A><BR></P></DIV>
<A NAME="Heading236"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Forgetting the object type</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This program might seem strange to you.
Why should anyone intentionally <I>forget</I> the type of an object? This is
what happens when you upcast, and it seems like it could be much more
straightforward if <B>tune(&#160;)</B> simply takes a <B>Wind</B> reference as
its argument. This brings up an essential point: If you did that, you&#8217;d
need to write a new <B>tune(&#160;)</B> for every type of <B>Instrument</B> in
your system. Suppose we follow this reasoning and add <B>Stringed</B> and
<B>Brass</B> instruments:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c07:music2:Music2.java </font>
<font color=#009900>// Overloading instead of upcasting.</font>

<font color=#0000ff>class</font> Note {
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> value;
  <font color=#0000ff>private</font> Note(<font color=#0000ff>int</font> val) { value = val; }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> Note
    MIDDLE_C = <font color=#0000ff>new</font> Note(0), 
    C_SHARP = <font color=#0000ff>new</font> Note(1),
    B_FLAT = <font color=#0000ff>new</font> Note(2);
} <font color=#009900>// Etc.</font>

<font color=#0000ff>class</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Instrument.play()"</font>);
  }
}

<font color=#0000ff>class</font> Wind <font color=#0000ff>extends</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Wind.play()"</font>);
  }
}

<font color=#0000ff>class</font> Stringed <font color=#0000ff>extends</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Stringed.play()"</font>);
  }
}

<font color=#0000ff>class</font> Brass <font color=#0000ff>extends</font> Instrument {
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> play(Note n) {
    System.out.println(<font color=#004488>"Brass.play()"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Music2 {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> tune(Wind i) {
    i.play(Note.MIDDLE_C);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> tune(Stringed i) {
    i.play(Note.MIDDLE_C);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> tune(Brass i) {
    i.play(Note.MIDDLE_C);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Wind flute = <font color=#0000ff>new</font> Wind();
    Stringed violin = <font color=#0000ff>new</font> Stringed();
    Brass frenchHorn = <font color=#0000ff>new</font> Brass();
    tune(flute); <font color=#009900>// No upcasting</font>
    tune(violin);
    tune(frenchHorn);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This works, but there&#8217;s a major
drawback: You must write type-specific methods for each new <B>Instrument</B>
class you add. This means more programming in the first place, but it also means
that if you want to add a new method like <B>tune(&#160;)</B> or a new type of
<B>Instrument</B>, you&#8217;ve got a lot of work to do. Add the fact that the
compiler won&#8217;t give you any error messages if you forget to overload one
of your methods and the whole process of working with types becomes
unmanageable. 

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久综合九色综合97婷婷| 看电视剧不卡顿的网站| 播五月开心婷婷综合| 久久网站最新地址| 国产麻豆精品theporn| 久久一二三国产| 成人黄色777网| 亚洲精品v日韩精品| 91成人在线免费观看| 天堂午夜影视日韩欧美一区二区| 51午夜精品国产| 国产综合久久久久久鬼色| 中日韩av电影| 欧美色综合久久| 精品一区二区在线看| 欧美激情一区二区三区在线| 91在线你懂得| 日本在线播放一区二区三区| 国产视频911| 欧洲av在线精品| 麻豆成人免费电影| 中文字幕av不卡| 欧美日韩色一区| 国产一区二区福利视频| 日韩伦理免费电影| 欧美一级艳片视频免费观看| 国产成人精品免费| 午夜免费久久看| 日本一区二区三区在线观看| 欧美中文字幕一二三区视频| 国产一区不卡精品| 亚洲综合另类小说| 久久久噜噜噜久久人人看 | 91精品国产综合久久久蜜臀粉嫩 | 久久综合狠狠综合| 在线观看一区不卡| 国产精品亚洲成人| 日韩成人一级片| 成人免费一区二区三区在线观看| 欧美一区二区福利在线| jlzzjlzz亚洲日本少妇| 麻豆成人免费电影| 一区二区三区免费观看| 国产日韩精品一区| 日韩视频一区二区在线观看| 91片在线免费观看| 国产精品小仙女| 裸体在线国模精品偷拍| 一区二区三区日韩在线观看| 国产欧美日韩视频一区二区| 日韩一区二区免费在线电影| 色老汉av一区二区三区| 成人免费不卡视频| 国产在线视频一区二区| 日韩精品电影在线观看| 亚洲午夜电影网| 中文字幕在线观看一区| 久久久国产精品不卡| 日韩精品最新网址| 欧美日本乱大交xxxxx| 在线影视一区二区三区| av动漫一区二区| 成人妖精视频yjsp地址| 国产一区二区三区久久悠悠色av| 男女男精品视频| 三级欧美韩日大片在线看| 亚洲亚洲人成综合网络| 亚洲一线二线三线久久久| 亚洲视频一二三| 亚洲日韩欧美一区二区在线| 国产精品日产欧美久久久久| 国产清纯在线一区二区www| 久久只精品国产| 久久久久久久久久美女| 久久精品欧美日韩精品| 久久久久久久久岛国免费| 久久久久久久av麻豆果冻| 2欧美一区二区三区在线观看视频| 日韩网站在线看片你懂的| 日韩一级片在线观看| 日韩欧美一二三四区| 精品免费日韩av| 国产丝袜欧美中文另类| 中文字幕巨乱亚洲| 亚洲欧洲日产国码二区| 亚洲免费高清视频在线| 亚洲国产精品一区二区尤物区| 亚洲午夜日本在线观看| 日韩二区在线观看| 久久国产尿小便嘘嘘| 国产一区在线视频| 粉嫩一区二区三区在线看| 色综合中文字幕国产 | 欧美在线观看一区| 欧美人与禽zozo性伦| 日韩视频一区二区三区 | 国产丝袜美腿一区二区三区| 国产精品午夜春色av| 亚洲激情自拍视频| 蜜桃一区二区三区四区| 国产精品一区二区91| 97se亚洲国产综合自在线观| 在线观看不卡视频| 精品久久国产字幕高潮| 中文字幕中文在线不卡住| 亚洲sss视频在线视频| 韩国在线一区二区| 色综合久久综合网欧美综合网| 欧美日韩夫妻久久| 久久久久久久久蜜桃| 亚洲免费观看高清完整版在线观看| 视频一区欧美日韩| 国产精品资源网站| 欧美午夜理伦三级在线观看| 日韩美女一区二区三区| 综合在线观看色| 另类小说视频一区二区| 波多野结衣的一区二区三区| 91.com视频| 中文字幕日韩一区| 久久99国产精品免费网站| 99久久精品久久久久久清纯| 91精品久久久久久久91蜜桃 | 欧美国产一区在线| 亚洲午夜久久久久久久久久久| 狠狠色狠狠色合久久伊人| 91国偷自产一区二区三区成为亚洲经典| 日韩视频在线永久播放| 亚洲欧美日韩国产一区二区三区| 看电影不卡的网站| 欧美日韩精品一区二区三区| 国产欧美一区二区三区沐欲| 日韩av一区二| 日本韩国精品在线| 国产三级一区二区三区| 日本强好片久久久久久aaa| 91视频国产观看| 国产欧美一区二区精品婷婷| 天堂午夜影视日韩欧美一区二区| 93久久精品日日躁夜夜躁欧美| 亚洲精品在线一区二区| 亚洲一二三四区不卡| av一二三不卡影片| 久久久精品tv| 久久国产精品色婷婷| 在线免费观看视频一区| 亚洲天堂久久久久久久| 成人免费高清在线观看| 久久久久久毛片| 国产一区二区三区免费播放 | 久久99深爱久久99精品| 欧美日韩在线三区| 怡红院av一区二区三区| 99视频精品免费视频| 国产欧美日韩综合| 国产成人综合网站| 国产丝袜欧美中文另类| 国产一区二区三区美女| 久久综合狠狠综合久久综合88| 久久精工是国产品牌吗| 日韩欧美电影在线| 美女www一区二区| 日韩欧美高清一区| 精品无人区卡一卡二卡三乱码免费卡| 在线成人av网站| 日韩国产欧美在线观看| 欧美一区二区免费视频| 麻豆成人av在线| 精品国产乱码久久久久久闺蜜 | 精品1区2区3区| 天堂午夜影视日韩欧美一区二区| 欧美日韩亚洲综合一区二区三区| 一区二区激情视频| 欧美日韩日本视频| 免费人成在线不卡| 欧美大片一区二区| 久久国产人妖系列| 日韩精品一区二区三区视频在线观看| 青青青爽久久午夜综合久久午夜| 欧美一级日韩免费不卡| 国产自产2019最新不卡| 日本一二三不卡| 色欲综合视频天天天| 亚洲小少妇裸体bbw| 欧美精品在线观看播放| 麻豆精品视频在线观看| 久久久噜噜噜久噜久久综合| av成人免费在线| 亚洲国产视频一区| 欧美sm极限捆绑bd| 成人手机电影网| 亚洲午夜免费电影| 精品国产乱码久久久久久1区2区| 成人性生交大片免费看视频在线 | 韩国一区二区三区| 国产精品福利电影一区二区三区四区 | www.日韩av| 日韩激情av在线| 欧美激情一区二区三区不卡 | 亚洲免费毛片网站|