题解 | #修改属性1#

修改属性1

https://www.nowcoder.com/practice/c5ab441e5e8f4ec7bf36ba51a3141340

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def printclass(self):
        try:
            self.age
        except AttributeError:
            print("Error! No age")
        else:
            print(f"{self.name}'salary is {self.salary}, and his age is {self.age}")

# 创建 Employee 实例 e
e = Employee(input(), int(input()))

# 调用 printclass 方法输出信息
e.printclass()

# 根据输入的年龄为实例 e 添加属性 age,并再次调用 printclass 方法输出信息
age = int(input())
e.age = age
e.printclass()

全部评论

相关推荐

11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务