本文共 2676 字,大约阅读时间需要 8 分钟。
面向对象思想起源
Alan Kay提倡应该关注消息机制和模块间的松耦合和交互,而不是模块内部的对象组成。非凡可成长系统的重中之重是设计好模块间如何通讯而不是模块具有什么样的内部实行和行为方法
软件功能的完成
- 类 定义了对象群体的逻辑结构,包括属性和操作 系统运行时,类作为产生对象的模板,在物理层面是不存在的
- 对象 系统运行时必须为每一个需要的对象分配内存、保存数据 对象存在于物理层面,每个对象都有自己的数据空间(内存) 所有的对象共享同一块代码空间
- 消息 对象之间的一种交流手段
- 有相关对象之间相互协作完成软件功能
类和对象
- Everything is an object
- A program is a bunch of objects telling each other what to do by sendingmessages.
- Each object has its own memory made up of other
- objects Every object has a type Each object is an instance of a
- class, in which “class” is synonymous with “type.” All objects of a
- particular type can receive the same messages.
面向对象的思考方式
对世界系统进行观察建模的时候,把他们看成由一系列相互交流,互为影响的对象集
- 世界是相互作用的对象组成的
- 描述由构建对象组成的系统 面向对象适合解决不确定的时间,创新性的时间(篮球赛) 面向结果适合处理一致的时势,重要的条件都已知的场景(流水线)
- 比较:通信 过程化解决方法通过信道传递数据,服务器端需要有专门的工具对接受的数据进行处理 面向对象对象解决方法通过信道传递(对象+对数据的处理方法)
核心特征
- Object / Class 类
- Method / Message 方法
- Encapsulation 封装
- Inheritance 继承
- Interface / Implement 接口
- Polymorphism 多态
- Composition / aggregation 组合
- Abstraction 抽象
- OO principle 面向对象原则
- design pattern 设计模式
Encapsulation 封装
- Encapsulationis the process of hiding the implementation details of an obiect
- The internal state is usually not accessible by other objects
- The only access to manipulate the obiect data is throuah its interface
- Encapsulation allows objects to be viewed as 'black boxes’
- It protects an obiect’s internal state from being corrupted by other objects
- Also, other objects are protected from changes in the object implementation.
- Communication is achieved through an 'interface’
- 封装的四种方式:
- public
- private
- protected
- package
Inheritance 继承
- A class gets the state and behacior of another class and adds additional state and behavior.
polymorphism 多态
- “When one class inherits from another, then polymorphismallows a subclass to stand in for the superclass.” 当一个类从另一个类继承而来,多态使得子类可以代替父类
- The sender of a stimulus doesn’t need to know the receiver’sclass 消息发送方不需要知道消息接收方属于那个子类
- Different receivers can interpret the message in their own way 同一类族的接收者可以按自己的方式处理消息
推论
- different receivers can response to the same stimulus based on theirinterpretation 同一类族的接收者可以按自己的方式处理同一个消息
- So we can have a variety of objects who process the same piece of data indifferent ways 有多种对象可以按自己的方式处理相同的数据
Aggregation/Composition 聚合/组合
- Inheritance means that one class inherits the characteristicsof another class. This is also called a “is a” relationship: A car is a vehicle A student is a person
- Aggregation describes a “has a” relationship. One object is apart of another obiect. A car has wheels A person has legs
- Compositon is a type of Strong aggregation
Interface/Implementation 接口/实现
- 软件系统内部是由大量的相互关联的类构成
- 对其中的一个类的局部进行修改时,不能影响其他类
- 接口(interface)描述与一个类的用户如何与这个类交互
- 实现(inplementation)完成接口所定义的功能
转载地址:http://bzksi.baihongyu.com/