当前位置: 首页 > 工具软件 > age > 使用案例 >

创建一个Person类,在构造函数中初始化姓名name、年龄age属性,创建有返回值的get_name方法获取人的姓名,有返回值的get_age函数获取人的年龄。

宇文鸿畴
2023-12-01

创建一个Person类,在构造函数中初始化姓名name、年龄age属性,创建有返回值的get_name方法获取人的姓名,有返回值的get_age函数获取人的年龄。再创建Student类继承Person类的属性和方法,在构造函数中调用基类的构造函数初始化共有的name、age属性,并将Student类独有的成绩属性course(包括语文、数学、英语三门成绩)进行初始化。创建有返回值的get_MaxScore方法用来返回3门科目中的 最高分数。使用实例s1 = Student(“小明”,18,[93,68,76])对Student类的三种方法进行测试,并输出结果。

程序:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name(self):
        return f"姓名为:{self.name}"

    def get_age(self):
        return f"年龄是:{self.age}"


class Student(Person):
    def __init__(self, name, age, course):
        super().__init__(name, age)
        self.course = course

    def get_MaxScore(self):
        return f"最高分是{max(self.course)}"


s1 = Student("小明", 18, [93, 68, 76])
print(s1.get_name())
print(s1.get_age())
print(s1.get_MaxScore())

结果:

姓名为:小明
年龄是:18
最高分是93

 类似资料: