题解 | #修改属性2#
修改属性2
https://www.nowcoder.com/practice/7831806be5684ad6ac0f8b43e3e3558e
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}")
n = input()
s = int(input())
a = int(input())
e = Employee(n,s)
if hasattr(e,'age'): # hasattr()判断对象是否有该属性
e.printclass()
else:
print(hasattr(e,'age'))
setattr(e,'age',a) # 给对象加上该属性,并且该属性的值也要加上去
e.printclass()

查看28道真题和解析