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)
python面向对象中的重写
最新推荐文章于 2024-04-10 14:58:27 发布