current position:Home>Python final exam SOS

Python final exam SOS

2022-02-02 23:50:06 CSDN Q & A

Define a class that represents the information of ordinary people Person And subclasses derived from it as the base class Student, Requirements are as follows :
(1) class Person Member variables of : name Indicate name ; age It means age ; sex Gender ;
(2) class Person Member method of : getName() : Get a name ; getAge( ): Age of acquisition ; getSex0: Get gender ; setName(): Reassign the name ; setAgeO : Re assign age ; setSexO: Reassign gender ;
(3) By class Person Derived subclass Student, It contains 2 Unique attributes - major (major) And College (school); 4 A unique member method is used to access professional and college properties , And reassign it , Namely getMaior(),setMajor(),getSchool (), setSchool();
(4) establish 1 individual Student Class object , Access each attribute information , And output the display .




Refer to the answer 1:
class Person(object):    def __init__(self):        self._name: str = ''        self._age: int = 0        self._sex: str = ''    def get_name(self):        return self._name    def set_name(self, name):        self._name = name    name = property(fget=get_name, fset=set_name)    def get_age(self):        return self._age    def set_age(self, age):        if isinstance(age, int):            self._age = age        else:            print(' Incorrect age format ')    age = property(fget=get_age, fset=set_age)    def get_sex(self):        return self._sex    def set_sex(self, sex):        self._sex = sex    sex = property(fget=get_sex, fset=set_sex)class Student(Person):    def __init__(self):        super().__init__()        self._major: str = ''        self._school: str = ''    def get_major(self):        return self._major    def set_major(self, major):        self._major = major    major = property(get_major, set_major)    def get_school(self):        return self._school    def set_school(self, school):        self._school = school    school = property(get_school, set_school)if __name__ == '__main__':    student = Student()    student.name = 'Bob'    student.age = 21    student.sex = ' male '    student.major = ' German '    student.school = ' College of Western Languages '    print(' Student {}  Age {}  Gender {}  major {}  college {}  Entered .'.format(student.name, student.age, student.sex,                                                 student.major, student.school))



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022350037890.html

Random recommended