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
时间: 2024-04-11 22:27:40 浏览: 192
输出为None是因为在getSex1方法中没有返回任何值。当没有明确返回值时,默认返回None。你可以在getSex1方法中添加return语句来返回性别。例如:
```python
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):
return super().getSex()
p1 = Teacher("Anna", 30, "woman", "English", 8000)
print(p1.getSex1())
```
这样就会输出"woman",表示性别为女性。
相关问题
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请帮我修改完善代码
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 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()
```
希望这对您有所帮助!
阅读全文