python设计一个人类Person,其内部有一个name属性,一个call()方法,输出姓名。 再设计一个学生类Student,继承自Person类,学生类有一个自己的属性stu_number学号。重写__init__方法和call方法,输出学号和姓名。
时间: 2024-03-25 21:41:45 浏览: 72
详解Python中的__new__、__init__、__call__三个特殊方法
好的,以下是代码实现:
```python
class Person:
def __init__(self, name):
self.name = name
def call(self):
print(f"My name is {self.name}")
class Student(Person):
def __init__(self, name, stu_number):
super().__init__(name)
self.stu_number = stu_number
def call(self):
print(f"My name is {self.name}, and my student number is {self.stu_number}")
```
使用方法如下:
```python
p = Person("Tom")
p.call() # 输出 "My name is Tom"
s = Student("Jerry", "20210001")
s.call() # 输出 "My name is Jerry, and my student number is 20210001"
```
阅读全文