python学习笔记(18) 子类和父类的关系

#隐式继承(子类完全继承父类):父类有函数,但是子类没有时,
#就会发生子类的动作完全等同于父类上的动作


class parent(object):#父类
    def implicit(self):#父类里的函数
        print("parent:父辈,implicit:隐式")


class child(parent):#子类
    pass#子类留空


dad = parent()
son = child()


dad.implicit()
son.implicit()#子类直接调用父类的函数


parent().implicit()#不用变量也可以,用变量是为了美观和简便
child().implicit()




#显示覆盖(子类完全覆盖父类):在子类中定义一个相同名称的函数,
#可以让子类的函数在继承父类时有一个不同的行为


class parent(object):
    def override(self):
        print("parent override:父类的重写")


class child(parent):
    def override(self):#子类的函数名称和父类里的函数名字要一样
        print("child override:子类的重写")


dad = parent()
son = child()


dad.override()
son.override()#子类的函数覆盖了父类的函数,实现自己独有的动作




#(子类部分覆盖父类):在父类的函数运行之前或者运行之后,
#再覆盖子类的函数,达到子类部分覆盖父类函数的效果


class parent(object):
    def altered(self):
        print("parent altered:父类的改变")


class child(parent):
    def altered(self):
        print("子类覆盖父类的函数之前")#子类自己的动作
        
        super(child,self).altered()
        #super(子类名,self).父类函数名(),调用父类函数动作
        #完整解释:用“子类名”和“self”这两个参数
        #调用super函数,然后参数返回到父类函数中
        
        print("子类覆盖父类函数之后")#回到子类自己的动作


dad = parent()
son = child()


dad.altered()
son.altered()


#最终版本:
print("................")
class parent(object):
    def override(self):
        print("父类的重写")


    def implicit(self):
        print("父类的隐式")


    def altered(self):
        print("父类的改变")


class child(parent):
    def override(self):
        print("子类的重写")


    def altered(self):
        print("子类覆盖父类的函数之前")
        super(child,self).altered()
        print("子类覆盖父类函数之后")


dad = parent()
son = child()


dad.implicit()
son.implicit()
print("-"*20)
dad.override()
son.override()
print("-"*20)
dad.altered()
print("-"*20)
son.altered()




print("-"*20)


print("1、(完全覆盖)子类有和父类同名的函数时,用子类自己的函数内容")
print("2、(继承父类)子类用的函数名在子类中没有,而在父类中有时,用父类的函数内容")
print("3、(部分覆盖)子类和父类有同名的函数,但是子类函数里有super函数时,"
      "super这一行的函数用父类里的内容。非super同行的函数继续用子类自己的内容")


class child(parent):
    def __init__(self,stuff):
        self.stuff = stuff
        super(child,self).__init__()
#super常和__init__函数配合使用,对子类做一些事情,然后再对父类进行初始化




#合成模式,如下所示,child直接使用Other这个类,或者别的模块也可以,不是继承Other
#这里不是父子类child是Other的关系,而是child里面有Other的关系。


class Other(object):#一个父类
    def override(self):
        print("Other的重写")


    def implicit(self):
        print("Other的隐式")


    def altered(self):
        print("Other的改变")


class child(object):#也是一个独立的父类
    def __init__(self):
        self.other=Other()
        #对child这个大类做一个初始化,然后在child这个类里设置一个self.other代表Other()的变量


    def implicit(self):
        self.other.implicit()


    def override(self):
        print("子类的重写")


    def altered(self):
        print("子类覆盖之前")
        self.other.altered()
        print("子类覆盖之后")


son = child()


son.implicit()
son.override()
son.altered()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值