Python继承和重写代码示例:
class Person:
# 定义一个类def __init__(self, name, age):# Person类的构造方法print("Person 的构造方法")self.name = nameself.age = agedef say_age(self):# say_age方法print(self.name, "的年龄是", self.age)class Student(Person):# 继承Person类def __init__(self, name, age, score):# 重写构造方法print("Student 的构造方法")Person.__init__(self, name, age)self.score = scoredef say_score(self):# say_score方法print(self.name, "的年龄是", self.age, "的分数是", self.score)def say_name(self):# 重写name方法print("我的名字是:", self.name)s1 = Student("刘总", 18, 90)s1.say_name()s1.say_age()s1.say_score
发表评论 取消回复