Python是面向对象的还是过程化的?
是的,Python 既支持面 向 对 象也支持过 程 化 编 程 语 言,因为它是一种为通用编程设计的高级编程语言。Python 是一种多范例语言,可以用所有这些语言编写很大程度上是过程化、面向对象或函数式的程序或库。取决于你所说的函数式编程的含义。Python 确实有函数式语言的一些特征。
面向对象编程的概念,例如 Python 中的类、封装、多态、继承等等,使它成为一种面向对象编程语言。
类似地,我们可以使用 Python 中的循环、for、while 以及控制结构创建过程化程序。
示例
class Rectangle:
def __init__(self, length, breadth, unit_cost=0):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_perimeter(self):
return 2 * (self.length + self.breadth)
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
# breadth = 120 cm, length = 160 cm, 1 cm^2 = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s cm^2" % (r.get_area()))
print("Cost of rectangular field: Rs. %s " %(r.calculate_cost()))输出
Area of Rectangle: 19200 cm^2 Cost of rectangular field: Rs. 38400000
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP