zoukankan      html  css  js  c++  java
  • 使用super()继承 分类: python 小练习 2013-09-18 14:13 320人阅读 评论(0) 收藏

    __metaclass__=type #定义使用新式类

    class Bird:
        def __init__(self):
            self.hungry = True #父类中的属性hungry
        def eat(self):
            if self.hungry:
                print 'Ha...Ha'
                self.hungry=False
            else:

                print 'No,Thanks'


    class Pets:
        def __init__(self):
            self.name='pets'
        def eat(self):
            print 'I can eat plant'

    class songBird(Pets,Bird):
        def __init__(self,name='k-bird'):
            super(songBird,self).__init__() #使用super进行继承
            self.name = name
        def song(self):
            self.eat()  # 先吃饭,再唱歌,无可厚非......哈哈。高兴的别太早,注意此处是调用Pets类中的eat()方法
            print 'Hello,everyone, I am %s,Do I sing well?' % self.name

    sb=songBird('Penguis')

    sb.song()


    继承多个父类中的同一个方法(如调用Bird类、Pets类中共同的eat方法),则根据类名顺序调用第一个类中的方法。如果是class songBird(Pets,Bird),则songBird类中调用eat方法,调用的是Pets类中的eat方法;如果class songBird(Bird,Pets),则songBird类中调用eat方法,调用的是Bird类中的eat方法。



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    js实现页面跳转,location.href和location.replace和location.reload的区别
    Object.create()和new 创建对象的区别
    apply、call、bind区别、用法
    JavaScript toString() 函数详解
    javascript中this指针详解
    javascript中null 和 undefined的区别
    jQuery 图片轮流展示效果
    min-height 兼容
    css 命名规范
    移动端隐藏手机虚拟键盘
  • 原文地址:https://www.cnblogs.com/think1988/p/4628059.html
Copyright © 2011-2022 走看看