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())
时间: 2024-03-11 14:15:48 浏览: 126
这段代码定义了两个类:Person和Ustudent,Person类有name、PID、sex、age属性和getinfo、setinfo、getname、getPID、getsex、getage方法,Ustudent类继承了Person类,并新增了school属性和getschool、setschool方法。代码中还进行了一些属性设置和方法调用的操作,你想问什么问题呢?
相关问题
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): self.name = name self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
class Student(Person):
def __init__(self, name, age, major):
super().__init__(name, age)
self.major = major
def study(self):
print(f"I am studying {self.major}.")
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def teach(self):
print(f"I am teaching {self.subject}.")
s = Student("John", 20, "Computer Science")
s.introduce() # Output: My name is John and I am 20 years old.
s.study() # Output: I am studying Computer Science.
t = Teacher("Jane", 35, "Math")
t.introduce() # Output: My name is Jane and I am 35 years old.
t.teach() # Output: I am teaching Math.
阅读全文