注意点:
题目中的hasattr和setattr已经提前定义好,不需要我们自己创建,直接调用即可,格式为:
hasattr(object, attr_name),其中attr_name应为字符串(String)类型
setattr(object, attr_name, attr_value),其中attr_name应为字符串(String)类型
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def printclass(self): print(f"{self.name}'salary is {self.salary}, and his age is {self.age}") name = str(input()) salary = int(input()) age = int(input()) e = Employee(name, salary) if print(hasattr(e, "age")): e.printclass() else: setattr(e, "age", age) e.printclass()