南京大学计算机软件研究所

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ClassandObject马晓星南京大学计算机软件研究所南京大学计算机科学与技术系2020/2/13InstituteofComputerSoftwareNanjingUniversity2关于对象式语言本课程并不系统讲授Eiffel语言但应学会“欣赏”Eiffel语言(比较“纯”)它首先是一个对象式程序设计语言但强调从分析到设计到实现的平滑过渡本课程主要讨论基于“类”的面向对象语言,但另有一些语言并不基于“类”概念。2020/2/13InstituteofComputerSoftwareNanjingUniversity3OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity4OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity5Threeworlds客观世界DVD播放机Whatisreality?复杂性认识的主观性问题世界抽象!TellmenotwhatyouarebutwhatyouhaveAmodelofasubsetoftherealityDVD播放机计算机(软件)世界Amodelofamodelofasubsetofthereality2020/2/13InstituteofComputerSoftwareNanjingUniversity6现实世界问题世界软件世界RealityAbstract2020/2/13InstituteofComputerSoftwareNanjingUniversity7OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity8Class:thestaticstructure类是对象式软件系统的基本组成单元类的构成类的使用信息隐蔽设施模块与类型类如何构成系统?其它2020/2/13InstituteofComputerSoftwareNanjingUniversity9Class“Aclassisanabstractdatatypeequippedwithapossiblypartialimplementation.”DeferredandeffectiveclassButwhy?ChairofSoftwareEngineering10AverydeferredclassdeferredclassCOUNTERfeatureitem:INTEGERisdeferredend--Countervalueupis--Increaseitemby1.deferredensureitem=olditem+1enddownis--Decreaseitemby1.deferredensureitem=olditem–1endinvariantitem=0end2020/2/13InstituteofComputerSoftwareNanjingUniversity11ClassMoldandinstance类自身也能被当作对象么2020/2/13InstituteofComputerSoftwareNanjingUniversity12类的构成类名继承关系成员术语分类ChairofSoftwareEngineering13AbstractdatatypePOINTx:POINTREALy:POINTREAL:POINTREAL:POINTREALClassPOINT:Choosearepresentation(polar,cartesian)Inpolarrepresentation,andareattributes,xandyareroutines.yxChairofSoftwareEngineering14AsimpleclassclassPOINTfeaturex,y:REAL--Pointcartesiancoordinatesmove(a,b:REAL)is--Movebyahorizontallyandbybvertically.dox:=x+ay:=y+bendscale(factor:REAL)is--Scalebyfactor.dox:=factor*xy:=factor*yendChairofSoftwareEngineering15ClassPOINT(cont’d)distance(p:POINT):REALis--DistancetopdoResult:=sqrt((x–p.x)^2+(y–p.y)^2)endro:REALis--Distancetoorigin(0,0)doResult:=sqrt(x^2+y^2)endtheta:REALis--Angletohorizontalaxisdo…endendChairofSoftwareEngineering16TerminologyAclassischaracterizedbyfeatures.Featurescompriseattributes(representingdatafieldsofinstancesoftheclass)androutines(operationsoninstances).Routinesaresubdividedintoprocedures(effectontheinstance,noresult)andfunctions(result,normallynoeffect).Everyoperation(routineorattributecall)isrelativetoadistinguishedobject,thecurrentinstanceoftheclass.ChairofSoftwareEngineering17AlternativeterminologyAttributesarealsocalledinstancevariablesordatamember.Routinesarealsocalledmethods,subprograms,orsubroutines.Featurecall—applyingacertainfeatureofaclasstoaninstanceofthatclass—isalsocalledpassingamessagetothatobject.Thenotionoffeatureisparticularlyimportantasitprovidesasingletermtocoverbothattributesandroutines.Itisoftendesirablenottospecifywhetherafeatureisanattributeoraroutine—asexpressedbytheUniformAccessprincipleChairofSoftwareEngineering18FeaturecategoriesbyroleCommandQueryFeatureProcedureAttributeFunctionNoresultReturnsresultComputationMemoryChairofSoftwareEngineering19FeaturecategoriesbyimplementationProcedureAttributeFunctionRoutineReturnsresultNoresultFeatureMemoryComputationChairofSoftwareEngineering20FeaturecategoriesCommandQueryFeatureProcedureAttributeFunctionNoresultReturnsresultComputationMemoryRoutineReturnsresultNoresultFeatureMemoryComputationChairofSoftwareEngineering21UniformAccessbalance=list_of_deposits.total–list_of_withdrawals.totallist_of_depositslist_of_withdrawalsbalancelist_of_depositslist_of_withdrawals(A2)(A1)ChairofSoftwareEngineering22ThePrincipleofUniformAccessFacilitiesmanagedbyamodulemustbeaccessibletoclientsinthesamewaywhetherimplementedbycomputationorstorage.ChairofSoftwareEngineering23UniformaccessthroughfeaturecallToaccessapropertyofapointp1,thenotationisthesameregardlessoftherepresentation,e.g.p1.xwhichisapplicablebothincartesianrepresentation(xisanattribute)andinpolarrepresentation(xisafunctionwithoutarguments).Inthefirstcasethefeaturecallisasimplefieldaccess;intheseconditcausesacomputationtobeperformed.Thereisnodifferenceforclients(exceptpossiblyintermsofperformance).2020/2/13InstituteofComputerSoftwareNanjingUniversity24关于C++/Java的静态成员有力的设施但并非面向对象计算所必须?不修改静态数据成员的修改静态数据成员的破坏面向对象的“纯粹性”singletargetprinciple(seebelow)考虑类作为对象???2020/2/13InstituteofComputerSoftwareNanjingUniversity25类的使用类的使用有两种形式允引(classAisaclientofclassB)继承(classAisadescendantofclassB)Clientandsuppliera:SinCFeaturecalla.some_featureora.some_feature(…)orinfixoperatorsSingl

1 / 86
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功