少说话多写代码之Python学习048——类的成员(supper函数)

栏目: Python · 发布时间: 6年前

内容简介:继承父类的构造函数,前面我们在子类的构造函数中调用:父类__init__(self),调用父类的构造函数。这种做法称之为绑定未绑定的父类构造函数。在Python3.0中可以使用supper函数实现继承父类构造函数。输出

继承父类的构造函数,前面我们在子类的构造函数中调用:父类__init__(self),调用父类的构造函数。这种做法称之为绑定未绑定的父类构造函数。在 Python 3.0中可以使用supper函数

实现继承父类构造函数。

_metaclass_=type
class Bird:
    def __init__(self):
        self.hungry=True

    def eat(self):
        if self.hungry:
            print('开始吃,好好吃...')
            self.hungry=False
        else:
            print('吃饱了,不要了...')

b= Bird()
b.eat();
b.eat()

输出

开始吃,好好吃...
吃饱了,不要了...
class BBigBird(Bird):
    def __init__(self):
        super(BBigBird,self).__init__()
        self.sound='小燕子,穿花衣,年年春天来这里...'
    def sing(self):
        print(self.sound)

bg = BBigBird()
bg.sing()
bg.eat()

输出

小燕子,穿花衣,年年春天来这里...
开始吃,好好吃...
吃饱了,不要了...

效果和绑定未绑定的方法一样。

工程文件下载: https://download.csdn.net/download/yysyangyangyangshan/10802010


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Text Algorithms

Text Algorithms

Maxime Crochemore、Wojciech Rytter / Oxford University Press / 1994

This much-needed book on the design of algorithms and data structures for text processing emphasizes both theoretical foundations and practical applications. It is intended to serve both as a textbook......一起来看看 《Text Algorithms》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具