首页 > 其他 > 详细

面向对象中,用super来联系父类的函数

时间:2018-10-13 17:50:48      阅读:168      评论:0      收藏:0      [点我收藏+]
class Animal:
    def __init__(self):
        print("A构造方法")
        self.ty = "动物"
    def Other(self):
        print("Others")
        self.nn = "simon"


class Cat(Animal):

    def __init__(self):
        print("B构造方法")
        self.n = ""
        # super执行父类的构造方法,可以执行父类的任何方法
        super(Cat, self).__init__()
        super(Cat,self).Other()


c = Cat()
print(c.__dict__)

执行结果:

B构造方法
A构造方法
Others
{‘n‘: ‘猫‘, ‘ty‘: ‘动物‘, ‘nn‘: ‘simon‘}

面向对象中,用super来联系父类的函数

原文:https://www.cnblogs.com/xuwenwei/p/9783495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!