Contrast programming ideas: process-oriented object-oriented comparison
- Process for: write barrier based on business logic codes from top to bottom
- Functional: The function code is encapsulated into a function, not need to repeat the preparation of the future, the function can be called only
- Object-oriented: the classification and packaging of the function, allowing developers to "faster and better and stronger ..."
Process-oriented programming, although easy to learn to use, but it often requires a long period code to implement the specified functions, the development of the most common operation is to paste the copied, namely: copying the code block prior to implementation of the current required function, the code is redundant, low efficiency.
The following is the code for the example process:
while True:
if cpu利用率 > 90%:
#发送邮件提醒
连接邮箱服务器
发送邮件
关闭连接
if 硬盘使用空间 > 90%:
#发送邮件提醒
连接邮箱服务器
发送邮件
关闭连接
if 内存占用 > 80%:
#发送邮件提醒
连接邮箱服务器
发送邮件
关闭连接
Then came the programming function, the following sample code:
def 发送邮件(内容)
#发送邮件提醒
连接邮箱服务器
发送邮件
关闭连接
while True:
if cpu利用率 > 90%:
发送邮件('CPU报警')
if 硬盘使用空间 > 90%:
发送邮件('硬盘报警')
if 内存占用 > 80%:
发送邮件('内存报警')
Object-oriented programming is a programming method and ideology, way of implementation of this program need to rely on "class" and "object" to achieve, so, in fact, object-oriented programming is the use of "class" and "object".
"Class" is a template, the template can contain multiple function, the function is used to implement some of the features.
Object is an example of a template created in the class of functions can be performed by an object instance.
- class is a keyword indicating that the class
- Create an object, you can parentheses after the class name
ps: The first argument must be a class of self (for details see: a package such three characteristics), as defined in the Class called "methods."
Java and C # only supports object-oriented programming, and the python that is more flexible support for object-oriented programming also supports functional programming.
# 创建类
class Foo:
def Bar(self):
print 'Bar'
def Hello(self, name):
print 'i am %s' %name
# 根据类Foo创建对象obj
obj = Foo()
obj.Bar() #执行Bar方法
obj.Hello('wupeiqi') #执行Hello方法
It is worth noting that, when executed by a "method" to use functional programming and object-oriented programming simple way function than the object-oriented
- Object-oriented: [] [create objects through the object execution method]
- Functional programming: [Executive function]
The answer is to observe the above comparison is yes, then is not absolute, its different ways of programming for different scenarios.
Thus, functional programming scenario is: independent and non-shared data between the respective functions