(1.6)面向对象概述之面向对象的核心特征

1.“面向对象”思考方式的核心特征1

  • 封装 Encapsulation
  • 继承 Inheritance
  • 多态 Polymorphism

2.封装

  • is the process of hiding the implementation details of an object隐藏了一个对象的实现细节
    The internal state is usually not accessible by other objects 内部的状态也不为其他对象所访问
     The only access to manipulate the object data is through its interface 对象的数据只能通过接口去访问

  • Encapsulation allows objects to be viewed as ‘black boxes’
    封装使得对象可以被看成一个“黑盒子”

  • It protects an object’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包可见性

2.继承

  • The basic principle is simple:
    A class gets the state and behaviour of another class and adds additional state and behaviour
    在这里插入图片描述
  • How do you express inheritance in C++?
class Car : public Vehicle 
{
	public:
	...
};
We state the above relationship in several ways:
Car is "a kind of a" Vehicle
Car is "derived from" Vehicle
Car is "a specialized" Vehicle
Car is a "subclass" of Vehicle
Car is a "derived class" of Vehicle
Vehicle is the "base class" of Car
Vehicle is the "superclass" of Car

3.多态

  • 本意:有多种形态 “Having many forms”
    “When one class inherits from another, then polymorphism allows a subclass to stand in for the
    superclass.” 当一个类从另一个类继承而来,多态使得子类可以代替父类
    The sender of a stimulus doesn’t need to know the receiver’s class消息发送方不需要知道消息接收方属于那个子类
    Different receivers can interpret the message in their own way同一类族的接收者可以按自己的方式处理消息

  • 推论Consequence
    different receivers can response to the same stimulus based on their interpretation同一类族的接收者可以按自己的方式处理同一个消息
    So we can have a variety of objects who process the same piece of data in different ways.有多种对象可以按自己的方式处理相同的数据

  • Implication: both of these are legal statements
    Airplane plane = new Airplane()
    Airplane plane = new Jet()
    这是多态的核心思想,是设计模式的基础!
    “使用指向父类的指针或者引用,能够调用子类的对象”
    在这里插入图片描述

4.“面向对象”的思考方式的核心特征2

  • 聚合/组合 Aggregation/Composition
  • 接口/实现 Interface /Implementation
  • 抽象 Abstraction

5.聚合/组合

  • Inheritance means that one class inherits the characteristics of another class.
    This is also called a “is a” relationship:
    在这里插入图片描述

  • Aggregation describes a “has a” relationship. Oneobject is a part of another object.
    在这里插入图片描述

  • Aggregations indicate that
    one object contains a set of other objects
    如,大学 是由学生、老师组成的
    Aggregation relationships are transitive 传递的
    if A contains B an B contains C, then A contains C

  • Aggregation relationships are asymmetric 不对称的
    If A contains B, then B does not contain A

  • A variant of aggregation is composition which adds the property of existence dependency
    聚合关系的一个变种,称为组合。
    整体控制着部分的生命
    if A composes B, then if A is deleted, B is deleted
    如手掌与手指

  • Composition is a type of Strong aggregation
    ‘whole’ can control the life span of ‘portion’
    部分对象只能存在于整体对象之中,整体对象控制部分对象的生命周期

  • 有些时候,聚合与组合的关系不是很明确
    如,房间 与 门

6.接口/实现

  • 对于软件系统
    软件系统的内部是由大量的互相关联的类构成的
    当对其中某一个类的局部进行修改的时候,不能影响其它的类
  • 接口 interface
    describe how users of the class interact with the class 描述一个类的用户如何与这个类交互
  • 实现 Implementation
    完成接口所定义的功能,如类、构件等完成的任务
  • eg:TV、电源插座、发电厂之间的关系?
    TV 是 顾客、用户
    插座 是 接口
    发电厂 是实现
    在这里插入图片描述

7.抽象 Abstraction

  • 抽象是一种思维方式、一种思维能力

  • “抽取比较像的部分出来”

  • 客观世界的事物由各种各样的实体(对象)构成

  • 每个对象都有各自的内部状态和运动(状态)规律

  • 根据对象的属性和运动规律的相似性可以将对象分类

  • “继承” 的强大就在于它的抽象和组织技术

  • 定义
    “抽象表示一个对象与其他所有对象相区别的基本特征,因此提供了同观察者角度有关的清晰定义的概念界限。 ”
    即:抽象(abstraction)就是过滤掉对象的一部分特性和操作,直到只剩下你所需要的属性和操作。

  • 抽象与具体的比较
    你要到飞机场去。坐进出租车,该如何描述?
    A: 师傅,请送我去飞机场! -------抽象
    B: 师傅,走!出门左拐、直行、过桥、右转、直行、红绿灯左拐后继续直行……-----具体

  • 抽象是面向对象领域发现类的主要方法

发布了556 篇原创文章 · 获赞 140 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/u011436427/article/details/104225356