python面向对象中的重写

class Person(object):
#重写发生在继承之间
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __str__(self):
        return ("name:%s,age:%d"%(self.name,self.age))
    def show(self):
        print("我是Person的方法")

xiaoHong = Person("小洪",18)
xiaoHong.show()
print(xiaoHong)

print("----------------")

class Student(Person):
    # 重写发生在继承之间
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return ("name:%s,age:%d" % (self.name, self.age))
#当Student类中没有show方法时,就会调用Person父类中的show方法
    # def show(self):
    #     print("我是Student的方法")

xiaoMing = Person("小明", 20)
xiaoMing.show()
print(xiaoMing)

猜你喜欢

转载自blog.csdn.net/qq_42336700/article/details/81414295