亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

TIJ英文原版書籍閱讀之旅——Chapter Eight:Polymorphism

2019-11-15 00:36:06
字體:
來源:轉載
供稿:網友
TIJ英文原版書籍閱讀之旅——Chapter Eight:Polymorphism 2015-06-16 10:25 by 海峰 :), ... 閱讀, ... 評論, 收藏, 編輯

Thetwist

|_Method-callbinding

Connectingamethodcalltoamethodbodyiscalledbinding.Whenbindingisperformedbeforethe

Whenalanguageimplementslatebinding,theremustbesomemechanismtodeterminethetypeoftheobjectatruntimeandtocalltheappropriatemethod.Thatis,thecompilerstilldoesn'tknowtheobjecttype,butthemethod-callmechanismfindsoutandcallsthecorrectmethodbody.Thelate-bindingmechanismvariesfromlanguagetolanguage,butyoucanimaginethesomesortoftypeinformationmustbeinstalledintheobjects.

Allmethodbindinginjavauseslatebindingunlessthemethodisstaticorfinal(privatemethodareimplicitlyfinal).Thismeansthatordinarilyyoudon'tneedtomakeanydecisionaboutwhetherlatebindingwilloccur-ithappensautomatically.

|_Pitfall:"overriding"privatemethods

Here'ssomethingyoumightinnocentlytrytodo:

public class PrivateOverride{private void f() {System.out.println("private f()");}public static void main(String[] args){PrivateOverride po = new Derived();po.f(); }}class Derived extends PrivateOverride{public void f() {System.out.println("public f()");}}/*Output:private f()*/

Youmightreasonablyexpecttheoutputtobe"publicf()",butaprivatemethodisautomaticallyfinal,andisalsohiddenfromthederivedclass.SoDerived'sf()inthecaseisabrandnewmethod;it'snotevenoverloaded,sincethebase-classversionoff()isn'tvisibleinDerived.Ifyouwanttogetthe"publicf()",youcanmodifythatcodepo.f()to((Derived)po).f().

Theresultofthisisthatonlynon-privatemethodsmaybeoverridden,butyoushouldwatchoutfortheappearanceofoverridingprivatemethods,whichgeneratesnocompilerwarnings,butdoesn'tdowhatyoumightexpect.Tobeclear,youshoulduseadifferentnamefromaprivatebase-classmethodinyourderivedclass.

|_Pitfall:fieldsandstaticmethods

Onceyoulearnaboutpolymorphism,youcanbegintothinkthateverythinghappenspolymorphically.However,onlyordinarymethodcallscanpolymorphic.Ifyouaccessafielddirectly,thataccesswillberesolvedatcompiletime.

Althoughthisseemslikeitcouldbeaconfusingissue,inpracticeitvirtuallynevercomesup.Foronething,you'llgenerallymakeallfieldsprivateandsoyouwon'taccessthemdirectly,butonlyassideeffectsofcallingmethods,Inaddition,youprobablywon'tgivethesamenametobase-classfieldandaderived-classfield,becauseitsconfusing.

Ifamethodisstatic,itdoesn'tbehavepolymorphically,staticmethodsareassociatedwiththeclass,andnottheindividualobjects.

Constructorsandpolymorphism

|_Orderofconstructorcalls

Eventhoughconstructorsarenotpolymorphic(they'reactuallystaticmethods,butthestaticdeclarationisimplicit),it'simportanttounderstandthewayconstructorsworkincomplexhierarchiesandwithpolymorphism.

Aconstructorforthebaseclassisalwayscalledduringtheconstructionprocessforaderivedclass,chaininguptheinheritancehierarchysothataconstructorforeverybaseclassiscalled.

Theorderofconstructorcallsforacomplexobjectisasfollows:

1.Thebase-classconstructoriscalled.Thisstepisrepeatedrecursivelysuchthattherootofthehierarchyisconstructedfirst,followedbythenext-derivedclass,etc.,untilthemost-derivedclassisreached.

2.Memberinitializersarecalledintheorderofdeclaration.

3.Thebodyofthederived-classconstructoriscalled.

Theorderoftheconstructorcallsisimportant.Whenyouinherit,youknowallaboutthebaseclassandcanaccessanypublicandprotectedmembersofthebaseclass.Thismeansthatyoumustbeabletoassumethatallthemembersofthebaseclassarevalidwhenyou'remembersofallpartsoftheobjecthavebeenbuilt.Insidetheconstructor,however,youmustbeabletoassumethatallmembersthatyouusehavabeenbuilt.Theonlywaytoguaranteethisisforthebase-classconstructortobecalledfirst.Thenwhenyou'reinthederived-classconstructor,allthemembersyoucanaccessinthebaseclasshavebeeninitialized.Knowingthatallmembersarevalidinsidetheconstructorisalsothereasonthat,wheneverpossible,youshouldinitializeallmemberobject(thatis,objectsplacedintheclassusingcomposition)attheirpointofdefinitionintheclass.Ifyoufollowthispractice,youwillheapensurethatallbaseclassmembersandmembersobjectsofthecurrentobjecthavebeeninitialized.

|_Inheritanceandcleanup

Whenyouoverridedispose()(thenameIhavechosentousehere;youmaycomeupwithsomethingbetter)inaninheritedclass,it'simportanttoremembertocallthebase-classversionofdispose(),sinceotherwisethebase-classcleanupwillnothappen.

|_Behaviorofpolymorphicmethodsinsideconstructors

Conceptually,theconstructor'sjobistobringtheobjectintoexistence(whichishardlyanordinaryfeat).Insideanyconstructor,theentireobjectmightbeonlypartiallyformed-youcanonlyknowthatthebase-classobjectshavebeeninitialized.Iftheconstructorisonlyonestepinbuildinganobjectofaclassthat'sbeenderivedfromthatconstructor'sclass,thederivedpartshavenotyetbeeninitializedatthetimethatthecurrentconstructorisbeingcalled.Adynamicboundmethodcall,however,reaches"outward"intotheinheritancehierarchy.Itcallsamethodinaderivedclass.Ifyoudothisinsideaconstructor,youcallamethodthatmightman

Youcanseetheprobleminthefollowingexample:

class Glyph{void draw() {System.out.println("Glyph.draw()");}Glyph(){System.out.println("Glyph() before draw()");draw();System.out.println("Glyph() after draw()");}}class RoundGlyph extends Glyph{private int radius = 1;RoundGlyph(int r){radius = r;System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);}void draw(){System.out.println("RoundGlyph.draw(), radius = " + radius);}}public class PolyConstructors{public static void main(String[] args){new RoundGlyph(5);}}/* Output:Glyph() before draw()RoundGlyph.draw(), radius = 0Glyph() after draw()RoundGlyph.RoundGlyph(), radius = 5*/

Theorderofinitializationdescribedintheearliersectionisn'tquitecomplete,andthat'sthekeytosolvingthemystery.Theactualprocessofinitializationis:

1.Thestorageallocatedfortheobjectisinitializedtobinaryzerobeforeanythingelsehappens.

2.Thebase-classconstructorsarecalledasdescribedpreviously.Atthispoint,theoverriddendraw()methodiscalled(yes,beforetheRoundGlyphconstructoriscalled),whichdiscoversaradiusvalueofzero,duetoStep1.

3.Memberinitializesarecalledintheorderofdeclaration.

4.Thebodyofthederived-classconstructoriscalled.

Foravoidthisproblem,agoodguidelineforguidelineforconstructorsis,"Doaslittleaspossibletosettheobjectintoagoodstate,andifyoucanpossiblyavoidit,don'tcallanyothermethodsinthisclass."Theonlysafemethodstocallinsideaconstructorarethosethatarefinalinthebaseclass.(Thisalsoappliestoprivatemethods,whichareautomaticallyfinal.)Thesecannotbeoverriddenandthuscannotproducethiskindofsurprise.Youmaynotalwaysbeabletofollowthisguideline,butit'ssomethingtostrivetowards.

Covariantreturntypes

JavaSE5addscovariantreturntypes,whichmeansthatanoverriddenmethodinaderivedclasscanreturnatypederivedfromthetypereturnedbythebase-classmethod.

Designingwithinheritance

Ageneralguidelineis"Useinheritancetoexpressdifferencesinbehavior,andfieldstoexpressvariationsinstate".

(END_XPJIANG)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美巨乳美女视频| 日韩在线播放av| 国产丝袜一区二区三区免费视频| 成人精品在线观看| 亚洲第一中文字幕| 亚洲电影免费观看高清完整版在线| 91免费看片网站| 日韩在线视频中文字幕| 国产69精品久久久久久| 奇米影视亚洲狠狠色| 成人黄色午夜影院| 国产日韩换脸av一区在线观看| 欧美国产日韩一区| 日韩一区二区三区在线播放| 色狠狠av一区二区三区香蕉蜜桃| 欧美日本黄视频| 一本色道久久88综合日韩精品| 亚洲欧美激情视频| 国产精品电影一区| 精品亚洲国产成av人片传媒| 国模私拍一区二区三区| 国产亚洲视频在线观看| 国产91精品高潮白浆喷水| 欧美成人免费在线观看| 18一19gay欧美视频网站| 欧美一二三视频| 亚洲r级在线观看| 亚洲第一av网| 韩国精品美女www爽爽爽视频| 欧美成人精品不卡视频在线观看| 久久成年人免费电影| 一区二区三区日韩在线| 国产亚洲精品久久久久动| 97视频免费观看| 亚洲理论电影网| 日本成人黄色片| 在线成人激情黄色| 日韩欧美国产骚| 亚洲第一福利在线观看| 欧美国产日产韩国视频| 国产亚洲精品一区二555| 俺去亚洲欧洲欧美日韩| 国产日韩一区在线| 亚洲视频在线看| 精品欧美aⅴ在线网站| 国产精品国内视频| 国产成人久久久精品一区| 美女av一区二区三区| 国产精品日日摸夜夜添夜夜av| 亚洲欧美中文字幕| 中文字幕亚洲字幕| 久久伊人精品一区二区三区| 亚洲性夜色噜噜噜7777| 欧美在线一级va免费观看| 国产精品丝袜高跟| 亚洲无限乱码一二三四麻| 日韩欧美精品免费在线| 91欧美日韩一区| 一区二区在线视频| 欧美日韩电影在线观看| 一夜七次郎国产精品亚洲| 国产视频精品va久久久久久| 在线观看国产精品淫| 欧美理论片在线观看| 久久这里有精品| 疯狂做受xxxx高潮欧美日本| 色综合五月天导航| 国产精品午夜视频| 上原亚衣av一区二区三区| 日韩中文字幕不卡视频| 国产精品丝袜久久久久久不卡| 中文字幕亚洲图片| 国内外成人免费激情在线视频| 亚洲美女性视频| 精品视频久久久久久| www.欧美三级电影.com| 国产精品视频自在线| 久久99精品视频一区97| 97国产精品视频人人做人人爱| 成人国产精品一区| 欧美国产日韩一区二区在线观看| 国产精品免费看久久久香蕉| 亚洲人成欧美中文字幕| 黑人欧美xxxx| 久久人人看视频| 欧美极品在线视频| 国产一区二中文字幕在线看| 中文字幕在线精品| 色偷偷噜噜噜亚洲男人的天堂| 日韩精品极品在线观看| 揄拍成人国产精品视频| 国产精品网站入口| 国产99久久久欧美黑人| 琪琪亚洲精品午夜在线| 国产69精品久久久久9| 亚洲欧洲中文天堂| 成人性生交大片免费看视频直播| 成人性生交大片免费看小说| 国产一区二区日韩精品欧美精品| 亚洲色图五月天| 日韩在线观看免费av| 日韩一区二区av| 波霸ol色综合久久| 久久精品国产久精国产一老狼| 人妖精品videosex性欧美| 日韩精品在线免费播放| 亚洲欧美福利视频| 国产精品久久久久久亚洲影视| 日韩欧美国产成人| 欧美性猛交xxxx乱大交| 日韩极品精品视频免费观看| 精品露脸国产偷人在视频| 久热99视频在线观看| 国产精品高清在线| 日韩欧美国产高清91| 日韩在线观看av| 在线观看欧美www| 日韩成人中文字幕| 国产成人精品免高潮在线观看| 日韩一区在线视频| 91理论片午午论夜理片久久| 久久久久日韩精品久久久男男| 中文字幕亚洲天堂| 高清亚洲成在人网站天堂| 国产97在线播放| 成人黄色激情网| 亚洲成人网av| 日韩激情第一页| 国产精品免费电影| 成人福利视频网| 亚洲欧美一区二区三区情侣bbw| 亚洲欧美日韩一区二区三区在线| 国产精选久久久久久| 日本精品久久久久影院| 国产精品激情av电影在线观看| 国产精品大片wwwwww| 日本最新高清不卡中文字幕| 国产精品一区=区| 亚洲免费电影在线观看| 欧美日韩国产丝袜另类| 国产精品一二区| 午夜免费日韩视频| 亚洲国产成人爱av在线播放| 国产精自产拍久久久久久| 欧美第一淫aaasss性| 欧美性猛交xxxx富婆| 亚洲毛片在线免费观看| 91精品久久久久久久久不口人| 久久精品国产成人精品| 亚洲第一精品电影| 欧美亚洲激情视频| 成人做爽爽免费视频| 国产亚洲精品久久久优势| 91亚洲精品久久久| 91香蕉国产在线观看| 国产97在线|亚洲| 亚洲综合第一页| 欧美日韩国产在线| 中文字幕免费精品一区高清| 亚洲成人av在线| 91午夜理伦私人影院| 亚洲国产日韩欧美综合久久| 国产成人福利夜色影视| 亚洲网址你懂得|