1. 1. 摘录
  2. 2. 结语

最开始学习编程的时候,就感觉 OOP 非常难以理解。我接触的第一门编程语言是 JS,当时不仅对如何实现 OOP 不理解,更无法理解 OOP 的设计哲学。无法理解为什么要这么做。后来随着学习范围的拓展,还有基础知识不断积累,对于 OOP 又会有了新的理解。就像我在上一篇文章中说的那样,多回头去看看一些东西,你会发现一些新的内容,这些内容其实都是比较经典的基础知识,只有这些内容你理解了,你才能更快的学习新的东西,才能对计算机融会贯通。

其实想写这篇文章是因为看到了廖雪峰 python2.7 的教程中面向对象编程这个章节的内容之后,又去 google 了一下 OOP,看到了一篇文章写的不错,然后因为正好又在学习 python,所以又根据 google 的提示,搜了一下 python oop 的内容,又看到了一篇写的非常不错的文章,详细介绍了 OOP 的背景,以及 python 中 OOP 的实现等。这里就把这些文章写罗列下来,供以后深入了解和复习参考。

  1. object-oriented-programming-OOP
  2. Improve your Python: Python classes and OOP

摘录

廖雪峰的教程中,关于 OOP 的描述,有一些比较经典。

OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数。

面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行。为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度。

而面向对象的程序设计把计算机程序视为一组对象的集合,而每个对象都可以接收其他对象发过来的消息,并处理这些消息,计算机程序的执行就是一系列消息在各个对象之间传递。

… 面向对象的抽象程度又比函数要高,因为一个Class既包含数据,又包含操作数据的方法。

而且在这个章节中举得例子也非常的好,可以看得出面向过程和面向对象的区别来。面向对象其实就是相当于把数据和方法(或者叫函数)封装起来,而直接使用这些数据和方法,编程的过程变成了数据之间通信和操作数据的过程。而面向过程则是将一个任务分成多个小任务,一个一个去实现的过程。

Improve your python

What is the class keyword used for, exactly? Like its function-based cousin def, it concerns the definition of things. While def is used to define a function, class is used to define a class. And what is a class? Simply a logical grouping of data and functions (the latter of which are frequently referred to as “methods” when defined within a class).

这段话说了两个意思:第一,def 关键字用来定义函数,而 class 关键字用来定义类。第二,所谓 class,其实就是 logical grouping of data and functions。这个说法和上面廖雪峰的说法是一致的。这个定义里的关键的词就是 logical,也就是说一个 class 拥有数据和方法,但是这些数据和方法之间是有逻辑关系的,不是随意放在一起的。

What do we mean by “logical grouping”? Well, a class can contain any data we’d like it to, and can have any functions (methods) attached to it that we please. Rather than just throwing random things together under the name “class”, we try to create classes where there is a logical connection between things. …

这段话说的就是我刚才上面说的意思,class 内部的数据和方法是有关联的,而不是随意组合形成的。

Classes can be thought of as blueprints for creating objects. When I define a Customer class using the class keyword, I haven’t actually created a customer. Instead, what I’ve created is a sort of instruction manual for constructing “customer” objects….

这段话中有一句话对于 class 的定义描写的特别好,就是 instruction manual for constructing,就是说所谓的 class 并不是一个真实的东西,而是很多实例的抽象概念,是创建实例时候的说明手册一样,告诉你创建的时候会有什么样的数据,什么样的方法等等。这个比喻特别好。

后面就是关于 python 中应该如何具体的创建 class 的内容了,这里就不展开详细讲解了。

再说一下第一篇文章object-oriented-programming-OOP

Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.

这段话说的是,OOP 这个计算机哲学是围绕 object 的,而 OOP 相对的是传统的理解,就是对于 actions 的操作。因为过去人们都是把计算机当做一系列 actions 的操作。

The programming challenge was seen as how to write the logic, not how to define the data. Object-oriented programming takes the view that what we really care about are the objects we want to manipulate rather than the logic required to manipulate them.

还是这个意思,在计算机刚开始的时候,人们写程序的挑战是如何书写 logic 的 actions,而不是如何定义数据。而 OOP 的重点在于我们应该如何实现对象之间的交互,而不是如何书写过程 logic。

The first step in OOP is to identify all the objects the programmer wants to manipulate and how they relate to each other, an exercise often known as data modeling.

OOP 的第一步是确认我们需要操作哪些对象,然后就是这些对象之间是如何交互的。

结语

OOP 的概念一直是初学者的难点,需要不断地去看不同的语言,来更加深刻的理解 OOP。