码迷,mamicode.com
首页 > 编程语言 > 详细

Python Special Syntax 7:继承

时间:2014-07-06 23:19:57      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   工作   代码   

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

#-*-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:继承

标签:style   blog   color   for   工作   代码   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!