定义 Student 类,它有一个属性country(默认值设为“中国”),定义类方法 get_country 和 set_country分别用于显示和修改country 的值。先用get_country获取country的值并输出,然后用set_country将country 的值修改为input函数的输入,然后输出。 用例1: 输入: CHINA 输出: 中国 CHINA 用例2: 输入: PRC CHINA 输出: 中国 PRC CHINA
时间: 2024-02-19 07:02:49 浏览: 18
好的,以下是 Python 代码实现:
```python
class Student:
country = "中国"
@classmethod
def get_country(cls):
print(cls.country)
@classmethod
def set_country(cls, new_country):
cls.country = new_country
# 用例1
Student.get_country()
Student.set_country(input())
Student.get_country()
# 用例2
Student.get_country()
Student.set_country(input())
Student.get_country()
```
运行代码后,会先输出默认值"中国",然后根据输入值修改country属性,再次输出修改后的值。