【python3】类的继承,方法重构

子类可以调用父类的方法或者重构父类的方法。

见代码

#coding:utf-8

class father():
    def __init__(self,name,age):
        self.name = name 
        self.age = age
        
    def introduce(self):
        print('我的家在东北松花江上')

    def say(self):
        print("我有很多手艺")

class son(father):
    def __init__(self,name,age):
        super(son,self).__init__(name,age)
        self.name = name
        self.age = age 

    def introduce_s(self):
        self.introduce()    #父类方法的继承
        print('我是一名程序猿',"姓名"+self.name,"年龄"+str(self.age))    #添加新的方法

    def say(self):  #重构方法    
        print('我会写代码')

if __name__ == '__main__': #只有将该模块作为入口进入
    me = son('www',23)
    me.introduce()
    me.say()
    me.introduce_s()

不怕世界大,就怕格局小
原文地址:https://www.cnblogs.com/wangjian1226/p/10453720.html