资源描述:
《面向对象-继承笔记(object oriented - inheritance notes)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、面向对象-继承笔记(Objectoriented-inheritancenotes)Inheritancedesignskills:A.commontotheparentclasstowrite,personalitytosubclasswriteWhenoverriddenbytheB.method,thesemanticsofthemethodremainunchangedandonlychangesareachievedC.inheritancesatisfiestherelationofISAD.donoti
2、nheritforcodereuse.Principle:Althoughinheritanceisagoodcodereusetechnology,DonotinheritorinheritforcodereuseThekeyconditionistheparent-childrelationshiptoexistinISA.Suchas:ThereisaplayGamemethodintheComputerclass,PublicclassComputer{Public,void,playGame(){//...
3、}Othermethods//...},andobjectsoftheHumanclassneedtousethismethod.Thismethodcanbeeasilyusedifinheritanceisused,However,violationoftheconditionsfortheuseofinheritance.Suchas:Public,class,Human,extends,Computer{Public,void,play(){//directlycalltheparentclassPlayGa
4、me();}}Insteadofinheritingandreusingcode.Usingacombination/aggregationmodelPublic,class,Human{PrivateComputercomputer;PublicHuman(Computer,computer){This.computer=computer;}Public,void,play(){//...Computer.playGame();}}----------------------------------------po
5、lymorphicDifferentobjectsatthesamecompiletimetypeareavailableatruntimeDifferentbehaviors.Polymorphicconditions:A.wantstoinherit(theparent,childclass)B.shouldhavemethodrewriting(override)Polymorphicapplications:ThecompiletypeoftheA.objectwritestheparentclasswhen
6、everpossible----------------------------------------Keywordforobject-orientedadvancedfeatures1.staticandstaticdomainstaticThestatickeywordisamodifierthatexpressesthestatic.Itcanbemodified:A.propertyIndicatesthatthispropertyisastaticpropertyandthatthestaticprope
7、rtyisalsocalledClassvariables,whichpropertyclasseshave,andallsuchSharedbyinstances.ThegenericpropertyiscalledtheMemberVariableIsownedbyanobject,andaninstanceofeachclasshasone.Syntax:Themodifierstatictypeattributename[=initialvalue];Onthebasisofcommonattributes,
8、plusthestaticmodifier.Applicationofstaticattributes:Thinking?Ifyoudesignaproperty,makeitoutofvalueTheoutsideisintroducedandkept,neverrepeated.Cases:PublicclassAccount{Privat