class Person: def __init__(self, name, age): self.name = name self.age = age
时间: 2024-05-25 09:17:23 浏览: 179
python脚本输出人的姓名、年龄等
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.
阅读全文