ClassandObject马晓星南京大学计算机软件研究所南京大学计算机科学与技术系2020/2/13InstituteofComputerSoftwareNanjingUniversity2关于对象式语言本课程并不系统讲授Eiffel语言但应学会“欣赏”Eiffel语言(比较“纯”)它首先是一个对象式程序设计语言但强调从分析到设计到实现的平滑过渡本课程主要讨论基于“类”的面向对象语言,但另有一些语言并不基于“类”概念。2020/2/13InstituteofComputerSoftwareNanjingUniversity3OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity4OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity5Threeworlds客观世界DVD播放机Whatisreality?复杂性认识的主观性问题世界抽象!TellmenotwhatyouarebutwhatyouhaveAmodelofasubsetoftherealityDVD播放机计算机(软件)世界Amodelofamodelofasubsetofthereality2020/2/13InstituteofComputerSoftwareNanjingUniversity6现实世界问题世界软件世界RealityAbstract2020/2/13InstituteofComputerSoftwareNanjingUniversity7OutlineThreeworlds:Object-OrientedModelingClass:thestaticstructureObject:therun-timestructureObjectcreation:fromclassestoobjectsObjectreference:linkingupobjects2020/2/13InstituteofComputerSoftwareNanjingUniversity8Class:thestaticstructure类是对象式软件系统的基本组成单元类的构成类的使用信息隐蔽设施模块与类型类如何构成系统?其它2020/2/13InstituteofComputerSoftwareNanjingUniversity9Class“Aclassisanabstractdatatypeequippedwithapossiblypartialimplementation.”DeferredandeffectiveclassButwhy?ChairofSoftwareEngineering10AverydeferredclassdeferredclassCOUNTERfeatureitem:INTEGERisdeferredend--Countervalueupis--Increaseitemby1.deferredensureitem=olditem+1enddownis--Decreaseitemby1.deferredensureitem=olditem–1endinvariantitem=0end2020/2/13InstituteofComputerSoftwareNanjingUniversity11ClassMoldandinstance类自身也能被当作对象么2020/2/13InstituteofComputerSoftwareNanjingUniversity12类的构成类名继承关系成员术语分类ChairofSoftwareEngineering13AbstractdatatypePOINTx:POINTREALy:POINTREAL:POINTREAL:POINTREALClassPOINT:Choosearepresentation(polar,cartesian)Inpolarrepresentation,andareattributes,xandyareroutines.yxChairofSoftwareEngineering14AsimpleclassclassPOINTfeaturex,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…endendChairofSoftwareEngineering16TerminologyAclassischaracterizedbyfeatures.Featurescompriseattributes(representingdatafieldsofinstancesoftheclass)androutines(operationsoninstances).Routinesaresubdividedintoprocedures(effectontheinstance,noresult)andfunctions(result,normallynoeffect).Everyoperation(routineorattributecall)isrelativetoadistinguishedobject,thecurrentinstanceoftheclass.ChairofSoftwareEngineering17AlternativeterminologyAttributesarealsocalledinstancevariablesordatamember.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)ChairofSoftwareEngineering22ThePrincipleofUniformAccessFacilitiesmanagedbyamodulemustbeaccessibletoclientsinthesamewaywhetherimplementedbycomputationorstorage.ChairofSoftwareEngineering23UniformaccessthroughfeaturecallToaccessapropertyofapointp1,thenotationisthesameregardlessoftherepresentation,e.g.p1.xwhichisapplicablebothincartesianrepresentation(xisanattribute)andinpolarrepresentation(xisafunctionwithoutarguments).Inthefirstcasethefeaturecallisasimplefieldaccess;intheseconditcausesacomputationtobeperformed.Thereisnodifferenceforclients(exceptpossiblyintermsofperformance).2020/2/13InstituteofComputerSoftwareNanjingUniversity24关于C++/Java的静态成员有力的设施但并非面向对象计算所必须?不修改静态数据成员的修改静态数据成员的破坏面向对象的“纯粹性”singletargetprinciple(seebelow)考虑类作为对象???2020/2/13InstituteofComputerSoftwareNanjingUniversity25类的使用类的使用有两种形式允引(classAisaclientofclassB)继承(classAisadescendantofclassB)Clientandsuppliera:SinCFeaturecalla.some_featureora.some_feature(…)orinfixoperatorsSingl