首页 > 编程语言 > 详细

Python Special Syntax 7:继承

时间:2014-07-06 23:18:52      阅读:459      评论:0      收藏:0      [点我收藏+]

继续在上一节的类的基础上测试继承:

#-*-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,这个多继承让我真的是有些半天合不拢嘴,工作这么多年就没玩过多继承……!

Python Special Syntax 7:继承,布布扣,bubuko.com

Python Special Syntax 7:继承

原文:http://www.cnblogs.com/yanyuge/p/3822488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!