python之7-2类的继承与多态

时间:2014-07-18 00:30:57   收藏:0   阅读:507
#!/usr/bin/env python

# coding=utf-8

#定义一个父类人类

class humen(object):

    def __init__(self,name,eye=2,age=None):

        self.name = name

        self.eye = eye

        self.age = age

    def action(self):

        print "%s%u个eye,这个人已经有%u岁了" %(self.name,self.eye,self.age)



class father(humen):

    def action(self):

        print "我是%s,是一名父亲" %self.name

class son(father):

    def action(self):

        print "我是%s,是一名的儿子" %self.name

one = humen("one",2,20)

tom = father(‘tom‘)

david = son(‘david‘)

def actiont(hm):

    return hm.action()

actiont(one)

actiont(tom)

actiont(david)


aaa103439@aaa103439-pc:~/桌面/python$ python test7_类_多态.py 

one有2个eye,这个人已经有20岁了

我是tom,是一名父亲

我是david,是一名的儿子

#!/usr/bin/env python

# coding=utf-8

#定义一个父类人类

class humen(object):

    def __init__(self,name,eye=2,age=None):

        self.name = name

        self.eye = eye

        self.age = age

    def action(self):

        print "%s%u个eye,这个人已经有%u岁了" %(self.name,self.eye,self.age)



class father(humen):

    namef = []

    def __init__(self,name):

        self.name = name

        father.namef = name

    def action(self):

        print "我是%s,是一名父亲" %self.name

class son(humen):

    def action(self):

        print "我是%s,是%s的儿子" %(self.name,father.namef)

one = humen("one",2,20)

tom = father(‘tom‘)

david = son(‘david‘)

bob = father(‘bob‘)

micheal = son(‘micheal‘)

def actiont(hm):

    return hm.action()

actiont(one)

actiont(tom)

actiont(david)

actiont(bob)

actiont(micheal)


aaa103439@aaa103439-pc:~/桌面/python$ python test7_类_多态.py 

one有2个eye,这个人已经有20岁了

我是tom,是一名父亲

我是david,是bob的儿子

我是bob,是一名父亲

我是micheal,是bob的儿子

python之7-2类的继承与多态,布布扣,bubuko.com

原文:http://www.cnblogs.com/aaa103439/p/3851315.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!