• python学习-65 继承2-子类中调用父类的方法


                                                                                     子类中调用父类的方法

    1.子类继承了父类的方法,然后想进行修改,那么就需要在子类中调用父类的方法。

    2.方法一:父类名

    class School:
        Country = 'china'
        def __init__(self,name,address):
            self.name = name
            self.address = address
    
    
        def go(self):
            print('开学啦~~~!')
    
    
    class Qh(School):
        def __init__(self,name,address,phone):
            School.__init__(self,name,address)              # 调用父类的方法
            self.phone = phone
    
        def show_info(self):
            print('%s大学开学啦' % self.name)
            print(self.name,self.address,self.phone)
    s1 =Qh('清华','北京','0088-11100413')
    s1.show_info()
    s1.go()

    运行结果:

    清华大学开学啦
    清华 北京 0088-11100413
    开学啦~~~!
    
    Process finished with exit code 0

    2.super调用

    class School:
        Country = 'china'
        def __init__(self,name,address):
            self.name = name
            self.address = address
    
    
        def go(self):
            print('开学啦~~~!')
    
    
    class Qh(School):
        def __init__(self,name,address,phone):
            super().__init__(name,address)
            self.phone = phone
    
        def show_info(self):
            print('%s大学开学啦' % self.name)
            print(self.name,self.address,self.phone)
    s1 =Qh('清华','北京','0088-11100413')
    s1.show_info()
    s1.go()

    运行结果相同。

  • 相关阅读:
    OpenDaylight及Postman实现VxLAN网络实验
    OpenDaylight的C/S模式实验
    OpenFlow1.3协议基于Mininet部署与验证
    Open vSwitch流表应用实战
    Open vSwitch的VxLAN隧道网络实验
    Open vSwitch的GRE隧道实验网络
    Open vSwitch使用案例扩展实验
    Beta 冲刺(1/7)
    福大软工 · 第十次作业
    福大软工 · BETA 版冲刺前准备(团队)
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11778107.html
Copyright © 2020-2023  润新知