Python 面向对象
Python 面向对象 类创建: # class 类名: class Demo: """ Demo Doc (类的说明文档)""" pass # 类.__doc__ 查看说明文档 print(Demo.__doc__) Demo1(基本实例): # 类的创建 class Person: """ Person class, Record Base Information """ # 类变量 country = 'China' # 称 __init__ 或 __new__ 为构造方法 def __init__(self, name): self.name = name def speak(self): print(self.name, ' Speaking ... ') # 称 __del__ 为析构方法,在对象被销毁时执行 def __del__(self): print(self.name, ' Destroy ... ') person = Person('GetcharZp') # 类实例化成对象 person.speak() # 调用对象中的 speak 方法 print(person.name) # 访问对象中的属性 print(Person.country) # 通过类访问类变量 print(person.country) # 通过对象访问类变量 # getattr