Python:父类的多继承与多态

#父类的多继承
例:孩子继承父亲以及母亲的属性
#父亲属性

class Father(object) :
    def __init__(self,money):
        self.money = money
    def Running(self):
        print("Running")

#母亲的属性

class Mother(object) :
    def __init__(self,Nice):
        self.Nice = Nice
    def Speak(self):
        print("Speak")

#孩子继承父亲母亲属性

from father import Father
from mother import Mother
class Chail(Father,Mother) :
    def __init__(self,money,Nice):
        Father.__init__(self,money)   #继承父类的属性
        Monther.__init__(self,NIce)
        #继承父类的方法
        dad = Father()
        dad.Running()

#类的多态 :一种事物的多种形态
定义一个动物类,该类有两个子类,狗类和猫类

class Animal:
    def __init__(self,name):
        self.name=name
    def show(self):
        print(self.name+"在叫“)
class dog(Animal):
	def show(self):
        print('{}喵喵叫......'.format(self.name))
class cat(pet):
    def show(self):
        print('{}喵喵叫......'.format(self.name))
mydog=dog('二哈')
mydog.show()
mycat=cat('橘猫')
mycat.show()

猜你喜欢

转载自blog.csdn.net/Mr_zhangbz/article/details/83059758
今日推荐