Python中创建员工类Employee,属性有姓名name、能力值ability、年龄age(能力值100-年龄),方法有doWork(),该方法执行一次,该员工的能力值减5,创建str()方法,打印该员工的信息
时间: 2023-04-09 13:00:39 浏览: 391
解析C#中的分部类和分部方法
可以回答这个问题。以下是Python代码:
```python
class Employee:
def __init__(self, name, ability, age):
self.name = name
self.ability = ability
self.age = age
def doWork(self):
self.ability -= 5
def __str__(self):
return f"Name: {self.name}, Ability: {self.ability}, Age: {self.age}"
employee1 = Employee("John", 90, 30)
employee1.doWork()
print(employee1)
```
输出结果为:
```
Name: John, Ability: 85, Age: 30
```
阅读全文