class Person: def __init__(self, name, age): self.name = name self.age = age def setName(self, name): self.name = name def setAge(self, age): self.age = age def show(self): print(f'Name: {self.name}, Age: {self.age}') class Student(Person): def __init__(self, name, age, major): super().__init__(name, age) self.major = major def setMajor(self, major): self.major = major def show(self): super().show() print(f'Major: {self.major}') # 测试代码 p = Person('Alice', 20) p.show() p.setName('Bob') p.setAge(21) p.show() s = Student('Charlie', 22, 'Computer Science') s.show() s.setName('Dave') s.setAge(23) s.setMajor('Mathematics') s.show()
时间: 2023-06-10 10:05:39 浏览: 230
这是一个 Python 的类定义,包括 Person 类和 Student 类。Person 类定义了一个包含 name 和 age 属性的人类,以及一些方法用于设置和显示这些属性。Student 类继承于 Person 类,并添加了一个 major 属性,以及一个用于设置和显示该属性的方法。Student 类重载了 show 方法,以便可以同时显示所有属性。
相关问题
完善代码#include<iostream.h> void main() {class Person: def __init__(self, name, gender, age, profession): self.name = name self.gender = gender self.age = age self.profession = profession def set_name(self, name): self.name = name def set_gender(self, gender): self.gender = gender def set_age(self, age): self.age = age def set_profession(self, profession): self.profession = profession def display_name(self): print("Name:", self.name) def display_gender(self): print("Gender:", self.gender) def display_age(self): print("Age:", self.age) def display_profession(self): print("Profession:", self.profession) def display_all(self): self.display_name() self.display_gender() self.display_age() self.display_profession() class Student(Person): def __init__(self, name, gender, age, profession, school, major, grade): super().__init__(name, gender, age, profession) self.school = school self.major = major self.grade = grade def set_school(self, school): self.school = school def set_major(self, major): self.major = major def set_grade(self, grade): self.grade = grade def display_school(self): print("School:", self.school) def display_major(self): print("Major:", self.major) def display_grade(self): print("Grade:", self.grade) def display_all(self): super().display_all() self.display_school() self.display_major() self.display_grade() person1 = Person("Alice", "Female", 25, "Engineer") person1.display_all() student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore") student1.display_all() }
抱歉,这段代码不是 C++ 代码,而是 Python 代码。可以参考下面的完整代码:
```python
class Person:
def __init__(self, name, gender, age, profession):
self.name = name
self.gender = gender
self.age = age
self.profession = profession
def set_name(self, name):
self.name = name
def set_gender(self, gender):
self.gender = gender
def set_age(self, age):
self.age = age
def set_profession(self, profession):
self.profession = profession
def display_name(self):
print("Name:", self.name)
def display_gender(self):
print("Gender:", self.gender)
def display_age(self):
print("Age:", self.age)
def display_profession(self):
print("Profession:", self.profession)
def display_all(self):
self.display_name()
self.display_gender()
self.display_age()
self.display_profession()
class Student(Person):
def __init__(self, name, gender, age, profession, school, major, grade):
super().__init__(name, gender, age, profession)
self.school = school
self.major = major
self.grade = grade
def set_school(self, school):
self.school = school
def set_major(self, major):
self.major = major
def set_grade(self, grade):
self.grade = grade
def display_school(self):
print("School:", self.school)
def display_major(self):
print("Major:", self.major)
def display_grade(self):
print("Grade:", self.grade)
def display_all(self):
super().display_all()
self.display_school()
self.display_major()
self.display_grade()
person1 = Person("Alice", "Female", 25, "Engineer")
person1.display_all()
student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore")
student1.display_all()
```
这个代码定义了两个类:`Person` 和 `Student`,其中 `Student` 是 `Person` 的子类。`Person` 类有四个属性:姓名、性别、年龄、职业,以及相应的 getter 和 setter 方法。`Student` 类在 `Person` 的基础上增加了三个属性:学校、专业、年级,也有相应的 getter 和 setter 方法。同时,`Student` 类重写了 `display_all()` 方法,打印出所有属性,包括父类属性和子类属性。
在代码的最后,创建了一个 `Person` 对象和一个 `Student` 对象,分别调用了它们的 `display_all()` 方法,打印出所有属性。
class Person: def __init__(self,name,PID,sex,age): self.name=name self.PID=PID self.sex=sex self.age=age def getname(self): return self.name def getPID(self): return self.PID def getsex(self): return self.sex def getage(self): return self.age def getinfo(self): return self.name,self.PID,self.sex,self.age def setname(self,name): self.name=name def setID(self,PID): self.PID=PID def setsex(self,sex): self.sex=sex def setage(self,age): self.age=agedef setinfo(self,name,PID,sex,age): self.name=name self.PID=PID self.sex=sex self.age=age class Ustudent(Person): #重写构造函数 def __init__(self,name,PID,sex,age,school): super().__init__(name,PID,sex,age) self.school=school def getschool(self): return self.school #重写 getinfo 函数 def getinfo(self): return self.name,self.PID,self.sex,self.age,self.school #重写 setinfo 函数 def setinfo(self,name,PID,sex,age,school): #self.name=name #self.PID=PID #self.sex=sex #self.age=age super().setinfo(name,PID,sex,age) def setschool(self,school): self.school=school p=Person("zjhong","3307217305256314","M",19) print(p.getinfo()) s=Ustudent("zjh","3307217305256313","M",20,"湖州师院学院") p.setinfo("zhangjh","3307217305256315","F",18) print(s.getinfo()) print(p.getinfo()) s.setinfo("zhangjianh","3307217305256316","F",21,"湖州学院") print(s.getinfo()) #调用父类继承函数 setname s.setname("lisi") s.setschool("湖州师范学院") print(s.getinfo()) print(p.getinfo())
这段代码定义了两个类:Person和Ustudent,Person类有name、PID、sex、age属性和getinfo、setinfo、getname、getPID、getsex、getage方法,Ustudent类继承了Person类,并新增了school属性和getschool、setschool方法。代码中还进行了一些属性设置和方法调用的操作,你想问什么问题呢?
阅读全文