- Python 设计模式教程
 - Python 设计模式 - 主页
 - 介绍
 - Python 设计模式 - 要点
 - 模型视图控制器模式
 - Python 设计模式 - 单例
 - Python 设计模式 - 工厂
 - Python 设计模式 - 构建器
 - Python 设计模式 - 原型
 - Python 设计模式 - 外观
 - Python 设计模式 - 命令
 - Python 设计模式 - 适配器
 - Python 设计模式 - 装饰器
 - Python 设计模式 - 代理
 - 责任链模式
 - Python 设计模式 - 观察者
 - Python 设计模式 - 状态
 - Python 设计模式 - 策略
 - Python 设计模式 - 模板
 - Python 设计模式 - 享元
 - 抽象工厂
 - 面向对象
 - 面向对象的概念实现
 - Python 设计模式 - 迭代器
 - 词典
 - 列表数据结构
 - Python 设计模式 - 集
 - Python 设计模式 - 队列
 - 字符串和序列化
 - Python 中的并发
 - Python 设计模式 - 反
 - 异常处理
 
- Python 设计模式资源
 - 快速指南
 - Python 设计模式 - 资源
 - 讨论
 
Python 设计模式 - 面向对象
面向对象模式是最常用的模式。这种模式几乎可以在所有编程语言中找到。
如何实现面向对象模式?
现在让我们看看如何实现面向对象模式。
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))
输出
上面的程序生成以下输出
解释
该代码包括类属性和实例属性,它们根据输出的要求打印。有多种功能构成面向对象模式的一部分。这些功能将在下一章中解释。