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这样就可以直接添加了