继续在上一节的类的基础上测试继承:
#-*-coding:utf-8 import Syntax2 class Student(Syntax2.Person): def __init__(self,name,age): Syntax2.Person.__init__(self,name) Syntax2.Person.printName(self) print(‘age %s‘ % age) self.age=age def detail(self): print(‘Name %s, age:%d‘,(self.name,self.age)) stu=Student(‘zhangsan‘,19) stu.detail() print(Syntax2.Person.count) print(stu.count)
输出:
I‘m importing by some other guys.
zhangsan has been created
Name is zhangsan
age 19
(‘Name %s, age:%d‘, (‘zhangsan‘, 19))
2
1
接着看多继承,这个跟C#有很大的区别
#-*-coding:utf-8 import Syntax2 class Student(Syntax2.Person,list): def __init__(self,name,age): Syntax2.Person.__init__(self,name) Syntax2.Person.printName(self) print(‘age %s‘ % age) self.age=age def detail(self): print(‘Name %s, age:%d‘,(self.name,self.age)) stu=Student(‘zhangsan‘,19) stu.detail() print(Syntax2.Person.count) print(stu.count) stu.append(‘item1‘) stu.append(‘item2‘) for i in stu: print(‘Member: %s‘ % i),
输出:
I‘m importing by some other guys.
zhangsan has been created
Name is zhangsan
age 19
(‘Name %s, age:%d‘, (‘zhangsan‘, 19))
2
1
Member: item1 Member: item2
注意上述代码块中的append方法以及foreach,这个多继承让我真的是有些半天合不拢嘴,工作这么多年就没玩过多继承……!