类的集成经典案例

class animal:
    def eat(self):
        print('%s is eating.'%self.name)
    def drink(self):
        print('%s is dreaking.'%self.name)
    def shit(self):
        print('%s is shiting.'%self.name)
class cat(animal):
    def __init__(self,name):
        self.name = name
        self.bread =''
    def cry(self):
        print("miao  miao  miao ... ")

class dog(animal):
    def __init__(self,name):
        self.name = name
        self.bread ="dog"
    def cry(self):
        print('wang wang wang ... ')

c1 = cat('xiao hei mao')
c1.eat()

c2 = cat('xiao bai mao')
c2.drink()
c1.cry()

c2.cry()

d1 = dog('ge bi da huang gou.')
d1.eat()
d1.cry()

对于面向对象的继承来说,其实就是将多个类共有的方法提取到父类中,子类仅需继承父类而不必一一实现每个方法。

注:除了子类和父类的称谓,你可能看到过 派生类 和 基类 ,他们与子类和父类只是叫法不同而已。

猜你喜欢

转载自www.cnblogs.com/kaishirenshi/p/10040210.html