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