目录

面向对象(Object Oriented)

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

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

现在让我们看看如何实现面向对象的模式。

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))

输出 (Output)

上述程序生成以下输出

面向对象

说明 (Explanation)

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

↑回到顶部↑
WIKI教程 @2018