NP99. 修改属性2
描述
输入描述
输出描述
示例1
输入:
NiuNiu 4000 23
输出:
False NiuNiu'salary is 4000, and his age is 23
Python 3 解法, 执行用时: 28ms, 内存消耗: 4452KB, 提交时间: 2022-07-28
na = input('') sa = input() ag = input() class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def printclass(self): if hasattr(Employee,'age') == 'True': print("{}'salary is {}, and his age is {}".format(self.name, self.salary, self.age)) else: print('False') setattr(Employee,'age',ag) print("{}'salary is {}, and his age is {}".format(self.name, self.salary, self.age)) e = Employee(na, sa) e.printclass()
Python 3 解法, 执行用时: 28ms, 内存消耗: 4456KB, 提交时间: 2022-08-02
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 = input() salary = int(input()) age = int(input()) e = Employee(name,salary) if not hasattr(e,"age"): print(False) e.age = age e.printclass() else: print(True)
Python 3 解法, 执行用时: 28ms, 内存消耗: 4464KB, 提交时间: 2022-07-28
class Employee(object): def __init__(self, name, salary): self.name = name self.salary = salary def printcalss(self): try: return (f'{self.name}\'salary is {self.salary}, and his age is {self.age}') except: return('Error! No age') name, salary, age = input(), int(input()), int(input()) e = Employee(name, salary) print(hasattr(e,'age')) e.age = age print(e.printcalss())
Python 3 解法, 执行用时: 28ms, 内存消耗: 4468KB, 提交时间: 2022-08-04
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 = input() salary = int(input()) age = int(input()) e = Employee(name,salary) if not hasattr(e,"age"): print(False) e.age = age e.printclass() else: print(True)
Python 3 解法, 执行用时: 28ms, 内存消耗: 4472KB, 提交时间: 2022-07-30
class Employee(): def __init__(self,name,salary): self.name=name self.salary=salary def printclass(self): print("{}'salary is {}, and his age is {}".format(self.name,self.salary,self.age)) name=input() salary=int(input()) age=int(input()) e=Employee(name,salary) if hasattr(e,'age'): print(True) else: setattr(e,'age',age) print(False) e.printclass()