《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.