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()

运行结果相同。

猜你喜欢

转载自www.cnblogs.com/liujinjing521/p/11778107.html
今日推荐