以下是用@property方法实现设置练习题数量和获得练习题数量的Python代码:
```python
class PythonTextbook:
def __init__(self):
self._lesson_num = 0
self._exercise_num = 0
@property
def exercise_num(self):
return self._exercise_num
@exercise_num.setter
def exercise_num(self, value):
self._exercise_num = value
def set_lesson_num(self, value):
self._lesson_num = value
def info(self):
print(f"Python课件数量:{self._lesson_num}")
print(f"Python练习题数量:{self._exercise_num}")
# 实例化一个PythonTextbook对象
python_textbook = PythonTextbook()
# 将课件数量设置为24,练习题数量设置为300
python_textbook.set_lesson_num(24)
python_textbook.exercise_num = 300
# 调用info()方法,输出信息
python_textbook.info()
```
运行结果:
```
Python课件数量:24
Python练习题数量:300
```
还木有评论哦,快来抢沙发吧~