class Person: def __init__ (self,name,age,sex): self.name=name self.age=age self.sex=sex def personInfo(self): print("姓名:",self.name,"年龄:",self.age,"性别:",self.sex) class Student(Person): def __init__(se请帮我修改完善代码
时间: 2024-02-19 10:01:02 浏览: 79
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def personInfo(self):
print("姓名:", self.name, "年龄:", self.age, "性别:", self.sex)
class Student(Person):
def __init__(self, name, age, sex, school):
super().__init__(name, age, sex)
self.school = school
def studentInfo(self):
print("姓名:", self.name, "年龄:", self.age, "性别:", self.sex, "学校:", self.school)
# 示例调用
p1 = Person("张三", 20, "男")
p1.personInfo()
s1 = Student("李四", 18, "女", "清华大学")
s1.personInfo()
s1.studentInfo()
相关问题
class Person: def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def getSex(self): return self.sex class Teacher(Person): def __init__(self, name, age, sex, course, salary): Person.__init__(self, name, age, sex) self.course = course self.salary = salary def getSex1(self): super().getSex() p1 = Teacher("Anna", 30, "woman", "English", 8000) print(p1.getSex1()) 为什么输出None
输出为None的原因是因为在getSex1方法中,没有返回super().getSex()的返回值。在Python中,如果函数没有明确的返回语句,则默认返回None。所以,当调用p1.getSex1()时,返回的就是None。要修正这个问题,可以在getSex1方法中添加return语句,将super().getSex()的返回值返回出来。例如:
```python
def getSex1(self):
return super().getSex()
```
这样,调用p1.getSex1()就会返回"woman",而不是None。
class Person: def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def personInfo(self): print("姓名:",self.name,"年龄:",self.age,"性别:",self.sex) class Student: def __init__(self,xueyuan,banji,name,age,sex): super().__init__(name,age,sex) self.xueyan=xueyuan self.banji=banji self.xueyan=self.xueyan def personInfo(self): super().personInfo() print("学院:",self.xueyan,"班级:",self.banji) class Teacher: def __init__(self,collage,prefessional,name,age,sex): super().__init__(name,age,sex) self.collage=collage self.prefessional=prefessional def personInfo(self): super().personInfo() print("学院:",self.collage,"专业:",self.prefessional) def teachObj(self): return "今天讲了如何使用面对对象设计程序" person=Person("卢本伟",33,"男") person.personInfo() student=Student("现代信息产业学院","计科火链2202","蔡徐坤",14,"男") student.personInfo() teacher=Teacher("现代信息产业学院","智能阿里2202","陈伟霆",54,"男") teacher.personInfo()帮我完善代码
您的代码已经非常完整了,我只是注意到了两个小错误。首先,在学生类的构造函数中,您两次赋值了`self.xueyan`,第二次应该是`self.banji`。其次,在学生类的`personInfo`方法中,您打错了`xueyuan`的拼写,应该是`xueyuan`。以下是已经修正的代码:
```
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def personInfo(self):
print("姓名:", self.name, "年龄:", self.age, "性别:", self.sex)
class Student(Person):
def __init__(self, xueyuan, banji, name, age, sex):
super().__init__(name, age, sex)
self.xueyuan = xueyuan
self.banji = banji
def personInfo(self):
super().personInfo()
print("学院:", self.xueyuan, "班级:", self.banji)
class Teacher(Person):
def __init__(self, collage, prefessional, name, age, sex):
super().__init__(name, age, sex)
self.collage = collage
self.prefessional = prefessional
def personInfo(self):
super().personInfo()
print("学院:", self.collage, "专业:", self.prefessional)
def teachObj(self):
return "今天讲了如何使用面对对象设计程序"
person = Person("卢本伟", 33, "男")
person.personInfo()
student = Student("现代信息产业学院", "计科火链2202", "蔡徐坤", 14, "男")
student.personInfo()
teacher = Teacher("现代信息产业学院", "智能阿里2202", "陈伟霆", 54, "男")
teacher.personInfo()
```
希望这对您有所帮助!
阅读全文