题解 | #修改属性1#另一种方法
修改属性1
https://www.nowcoder.com/practice/c5ab441e5e8f4ec7bf36ba51a3141340
class Employee: def __init__(self, name, salary, age = -1): self.name = name self.salary = salary self.age = age def printclass(self): if(self.age == -1): print("Error! No age") else: print(f"{self.name}'salary is {self.salary}, and his age is {self.age}") name = input() salary = input() e = Employee(name, salary) e.printclass() age = int(input()) e.age = age e.printclass()
属性是可以动态的添加的,在最开始init的时候可以没有age的属性。如果要添加,可以直接e.age这样就可以直接添加了