ch3 面向对象编程.pdf

上传人:asd****56 文档编号:70322921 上传时间:2023-01-19 格式:PDF 页数:27 大小:241.29KB
返回 下载 相关 举报
ch3 面向对象编程.pdf_第1页
第1页 / 共27页
ch3 面向对象编程.pdf_第2页
第2页 / 共27页
点击查看更多>>
资源描述

《ch3 面向对象编程.pdf》由会员分享,可在线阅读,更多相关《ch3 面向对象编程.pdf(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、OO Programming OO Programming Ref.ch1&The Java Tutorials1.Object Oriented Principle2.Object and Classes3.Inheritance 4.Interface 3.1Object Oriented PrincipleProblem Description“customers are allowed to have different types of bank accounts,deposit money,withdraw money and transfer money between acco

2、unts”Procedural Approachbool MakeDeposit(int accountNum,float amount);float Withdraw(int accountNum,float amount);struct Account char*name;int accountNum;float balance;char accountType;Procedural Approach contdFocus is on proceduresAll data is shared:no protectionMore difficult to modifyHard to mana

3、ge complexityProcedural vs.Object-OrientedProceduralWithdraw,deposit,transferObject OrientedCustomer,money,accountMapping the world to softwareObjects in the problem domain are mapped to objects in software0111011001111101011010011010010101111010110101Object OrientedData and operations are grouped t

4、ogetherAccountWithdrawDepositTransferInterface:Set of available operationsData Encapsulationclass Account public:float withdraw();void deposit(float amount);private:float balance;);Advantages of EncapsulationProtectionConsistencyAllows changeAlan Kays Description of OOP(pg.2)1.Everything is an objec

5、t 2.Objects perform computation by making requests of each other through the passing of messages 3.Every object has its own memory,which consists of other objects.4.Every object is an instance of a class.A class groups similar objects.5.The class is the repository for behavior associated with an obj

6、ect(All objects of a particular type can receive the same messages)6.Classes are organized into singly-rooted tree structure,called an inheritance hierarchy.3.2Object and ClassesClasses reflect concepts,objects reflect instances that embody those concepts.DariaJaneBrittanyJodiegirlclassobjectObjects

7、 and Classes contdA class captures the common properties of the objects instantiated from itA class characterizes the common behavior of all the objects that are its instancesObjects and Classes contdClass BankAccountBalanceInterestYTDOwnerAccount_numberBalance 500InterestYTDOwner Account_numberBala

8、nce 10,000InterestYTDOwner Account_numberOperationsMakeDespositTransferWithDrawGetBalanceObjects as instances of ClassesThe world conceptually consists of objectsMany objects can be said to be of the same type or class?My bank account,your bank account,Bill Gatesbank account We call the object type

9、a class InstantiationAn Object is instantiated from a ClassBankAccount myAccount;myAccount=new BankAccount;Objects and ClassesClass?Visible in source code?The code is not duplicatedObject?Own copy of data?Active in running program?Occupies memory?Has the set of operations given in the class3.3 Inher

10、itance A class which is a subtype of a more general class is said to be inherited from it.The sub-class inherits the base classdata members and member functionsChecking AccountValue FirstSelect AccessFirst InterestSavings AccountAccountInheritance-ClassificationInheritance contdA sub-class has all d

11、ata members of its base-class plus its ownA sub-class has all member functions of its base class(with changes)plus its ownInheritance is meant to implement sub-typing(dont abuse it)AbstractionManagement of complexityHierarchical classification:(pg.8)is-a relationship:inheritancehas-a relationship:co

12、ntainment3.4 InterfaceAn interface is a contract between a class and the outside world.When a class implements an interface,it promises to provide the behavior published by that interface.PolymorphismOne interface,Multiple implementationsInheritanceMethod overloading?举例.Checking account vs.Savings a

13、ccountSummaryWhat is Object Oriented Programming?Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects,each of which represents an instance of some class,and whose classes are all members of one or more hierarchy of classes un

14、ited via inheritance relationshipsObject1Data1+Procedures1DataData1Object3Data3+Procedures3Object2Data2+Procedures2 Object4Data4+Procedures4OOP FeaturesEncapsulation(封装)?对象和类概念的主要特性。封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。Abstraction(抽象)?忽略主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。Inheritance(继承)?层次模型,允许和鼓励类的重用,它提供

15、了一种明确表述共性的方法。Polymorphism(多态)?允许不同类的对象对同一消息作出响应,获得不同的执行效果。Preliminary Java OO ProgrammingPreliminary Java OO Programming1.Real-world objects contain state and behavior.2.A software objects state is stored in fields.3.A software objects behavior is exposed through methods.4.Hiding internal data from

16、the outside world,and accessing it only through publicly exposed methods is known as data encapsulation.5.A blueprint for a software object is called a class.6.Common behavior can be defined in a superclass and inherited into a subclassusing the extends keyword.7.A collection of methods with no implementation is called an interface.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁