zoukankan      html  css  js  c++  java
  • 类的集成经典案例

    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()

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

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

  • 相关阅读:
    10多媒体
    胡凡-01
    概念
    算法
    07Axios
    05VueCli
    04Vue.js路由系统
    03生命周期
    《穷人思维》学习感悟
    《基金》学习感悟之二
  • 原文地址:https://www.cnblogs.com/kaishirenshi/p/10040210.html
Copyright © 2011-2022 走看看