Python中类的定制

 1 class Chinese:
 2     eye = 'black'
 3 
 4     def eat(self):
 5         print('吃饭,选择用筷子。')
 6 
 7 class Guangdong(Chinese):  # 类的继承
 8     native_place = 'guangdong'  # 类的定制
 9 
10     def dialect(self):  # 类的定制
11         print('我们会讲广东话。')
12 
13 yewen = Guangdong()
14 print(yewen.eye)
15 # 父类的属性能用
16 print(yewen.native_place)
17 # 子类的定制属性也能用
18 yewen.eat()
19 # 父类的方法能用
20 yewen.dialect()
21 # 子类的定制方法也能用

在子类下新建属性或方法,让子类可以用上父类所没有的属性或方法。这种操作,属于定制中的一种:新增代码。

原文地址:https://www.cnblogs.com/Through-Target/p/12118362.html