Python(3)——Python中的继承

1、Python和C++一样,支持多继承。
#coding:utf-8
class cook(object):
    def cooking(self):
        print 'cooking'
    def eat(self):
        print 'cook eat'
        
class programmer(object):
    def coding(self):
        print 'coding'
    def eat(self):
        print 'programmer eat' 
        
class Me(cook,programmer):
    pass

class You(programmer,cook):
    pass

me = Me()
me.cooking()
me.coding()
me.eat()

print '\n'

you = You()
you.cooking()
you.coding()
you.eat()
打印结果:
cooking
coding
cook eat

cooking
coding
programmer eat
Python多继承调用多态性方法的顺序是:深度优先。

我的博客其他文章列表  
http://my.oschina.net/helu






你可能感兴趣的:(Python(3)——Python中的继承)