2.定义一个identifer类,具有一一个属性id,存放身份证号,以及如下函数:(1) getyear 函数用于从身份证号码中提取出生年份: (2) disp 方法用于输出身份证号码. 编写利用该类的主程序。
时间: 2024-06-09 10:05:09 浏览: 5
以下是 `Identifier` 类的代码实现:
```python
class Identifier:
def __init__(self, id):
self.id = id
def getyear(self):
year = self.id[6:10]
return year
def disp(self):
print("身份证号码为:", self.id)
```
主程序如下:
```python
id = Identifier("110101199003071234")
year = id.getyear()
id.disp()
print("出生年份为:", year)
```
运行结果:
```
身份证号码为: 110101199003071234
出生年份为: 1990
```
相关问题
定义一个identifer类,具有一个属性id,存放一个身份证号,以及一个如下函数:(1)getyear函数用于从身份证中提取出身年份(2)Disp方法输出身份证号码 编写利用该类的Python主程序
以下是 `identifer` 类的实现:
```python
class identifer:
def __init__(self, id):
self.id = id
def getyear(self):
return int(self.id[6:10])
def Disp(self):
print("身份证号码:", self.id)
```
可以通过以下方式使用该类:
```python
# 创建对象
id1 = identifer("11010119900307267X")
# 调用函数
year = id1.getyear()
id1.Disp()
# 输出结果
print("出生年份:", year)
```
输出结果为:
```
身份证号码: 11010119900307267X
出生年份: 1990
```
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)