设计一个人类(CCPM),属性包括姓名、性别、身份证号、时间、 费用/月;方法包括构造方法实现属性的初始化,其他成员方法能够实现设置 属性值,获取属性值、输出属性值。
时间: 2023-10-20 11:08:05 浏览: 27
好的,这是一个关于设计一个人类的问题,我可以回答。以下是答案:
答案:可以使用面向对象的编程思想,定义一个 Person 类,其属性包括姓名、性别、身份证号、时间、费用/月;方法包括构造方法实现属性的初始化,其他成员方法能够实现设置属性值,获取属性值、输出属性值。示例代码如下所示:
```python
class Person:
def __init__(self, name, gender, id_card, time, cost):
self.name = name
self.gender = gender
self.id_card = id_card
self.time = time
self.cost = cost
def set_name(self, name):
self.name = name
def get_name(self):
return self.name
def set_gender(self, gender):
self.gender = gender
def get_gender(self):
return self.gender
def set_id_card(self, id_card):
self.id_card = id_card
def get_id_card(self):
return self.id_card
def set_time(self, time):
self.time = time
def get_time(self):
return self.time
def set_cost(self, cost):
self.cost = cost
def get_cost(self):
return self.cost
def output(self):
print("姓名:{}\n性别:{}\n身份证号:{}\n时间:{}\n费用/月:{}".format(
self.name, self.gender, self.id_card, self.time, self.cost))
```
使用该类可以创建一个人类的对象,设置属性值,获取属性值,输出属性值。示例代码如下所示:
```python
# 创建一个人类的对象
person = Person("zhangsan", "男", "123456789012345678", "2021-01-01", 1000)
# 输出属性值
person.output()
# 修改属性值
person.set_name("lisi")
person.set_cost(2000)
# 再次输出属性值
person.output()
# 获取属性值
print(person.get_name())
print(person.get_cost())
```
输出结果如下所示:
```
姓名:zhangsan
性别:男
身份证号:123456789012345678
时间:2021-01-01
费用/月:1000
姓名:lisi
性别:男
身份证号:123456789012345678
时间:2021-01-01
费用/月:2000
lisi
2000
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)