在面向过程结构化编程中,程序被分为“数据结构”和“算法”两部分,可以说,在C语言中,我们实际在操纵数据。而面向过程语言大大强化了“数据结构”这个概念,并让其达到了可称之为“对象”的水平。对象是由数据和允许的操作构成的封装体,从而面向对象语言更安全也更稳定。
在面向对象中,核心是"对象"即由数据和允许的操作构成的封装体,而面向过程中的核心为“算法”。
Procedural Programming can be defined as a programming model which is derived from structured programming, based upon the concept of calling procedure. Procedures, also known as routines, subroutines or functions, simply consist of a series of computational steps to be carried out. During a program’s execution, any given procedure might be called at any point, including by other procedures or itself.
面向过程是计算步的集合,从开始到结束(自顶而下),所有的函数都可调用,只为完成计算,因而不安全,其中函数比数据更重要,不基于现实世界,且数据不可变。
Object oriented programming can be defined as a programming model which is based upon the concept of objects. Objects contain data in the form of attributes and code in the form of methods. In object oriented programming, computer programs are designed using the concept of objects that interact with real world. Object oriented programming languages are various but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.
面向对象不在乎开始和结束(自底而上),其他与面向过程相反。
面向过程是主要关心在已有事物上加以新的操作,而面向对象主要在已有事物上添加新的类(对象)。
python代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
所有的对象都有特定的操作;因为类不断进行细分。对象并不是数据的结构,所以他有很大的运行效率问题,但是是很自然的,功能强大且符合人类习惯。
面向过程全部依靠与操作,所以一般人秀不出来。
面向过程和计算机的执行结构密切相关。移动值并明确表示操作。而面向过程往往只是给与一个意图,而非明确算法,可以进行高质量代码的优化。
可以认为OOP和POP本质是正交的。没有高级的操作便没有面向对象编程,面向对象实际割舍了大部分的面向过程性质。这两者无法成为统一体,因为效率和高速的开发不可兼得。