举 2019-12-30
class Chinese:
eye = ‘black‘
def eat(self):
print(‘吃饭,选择用筷子。‘)
class Guangdong(Chinese): # 类的继承
native_place = ‘guangdong‘ # 类的定制
def dialect(self): # 类的定制
print(‘我们会讲广东话。‘)
yewen = Guangdong()
print(yewen.eye)
# 父类的属性能用
print(yewen.native_place)
# 子类的定制属性也能用
yewen.eat()
# 父类的方法能用
yewen.dialect()
# 子类的定制方法也能用在子类下新建属性或方法,让子类可以用上父类所没有的属性或方法。这种操作,属于定制中的一种:新增代码。
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =