Python设计模式 专题
您的位置:python > Python设计模式专题 > 面向对象设计模式
面向对象设计模式
作者:--    发布时间:2019-11-20

面向对象的模式是最常用的模式。 几乎所有的编程语言都可以找到这种模式。

如何实现面向对象的模式?

下面让我们看看如何实现面向对象的模式。参考以下实现代码 -

class parrot:
   # class attribute
   species = "bird"

   # instance attribute
   def __init__(self, name, age):
      self.name = name
      self.age = age

# instantiate the parrot class
blu = parrot("blu", 10)
woo = parrot("woo", 15)

# access the class attributes
print("blu is a {}".format(blu.__class__.species))
print("woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))

执行上面示例代码,得到以下输出结果 -

说明
代码包括类属性和实例属性,它们按照输出的要求打印。有各种功能构成面向对象模式的一部分。 这些功能在下一章中介绍。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册