Python是面向对象还是过程式的?
是的,Python的支持Øbject导向 和过程式编程语言,因为它是一个高层次的编程语言设计的通用编程。Python是多范例的,您可以使用所有这些语言编写很大程度上是过程性,面向对象或功能性的程序或库。这取决于您所说的功能。Python确实具有功能语言的某些功能。
OOP在Python中的类,类,封装,多态性,继承等概念使之成为一种面向对象的编程语言。
以类似的方式,我们可以使用循环,for,while等..和控制结构通过python创建过程程序。
例
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
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话