zoukankan      html  css  js  c++  java
  • 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()

    运行结果相同。

  • 相关阅读:
    Java的代码风格
    哪些你容易忽略的C语言基础知识
    Java基础学习笔记第二章
    Java代码性能优化总结
    Java并发编程(2):线程中断(含代码)
    C语言代码训练(一)
    数控G代码编程详解大全
    PLC编程算法
    博客转移到新地址
    一些吐槽
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11778107.html
Copyright © 2011-2022 走看看