python自定义类并使用的方法
作者:work24 时间:2022-08-16 14:36:29
本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下:
class Person:
def __init__(self, first, middle, last, age):
self.first = first;
self.middle = middle;
self.last = last;
self.age = age;
def __str__(self):
return self.first + ' ' + self.middle + ' ' + self.last + \
' ' + str(self.age)
def initials(self):
return self.first[0] + self.middle[0] + self.last[0]
def changeAge(self, val):
self.age += val
myPerson = Person('Raja', 'I', 'Kumar', 21)
print(myPerson)
myPerson.changeAge(5)
print(myPerson)
print(myPerson.initials())
运行结果如下:
Raja I Kumar 21
Raja I Kumar 26
RIK
希望本文所述对大家的Python程序设计有所帮助。
标签:python,自定义类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python调用百度地图WEB服务API获取地点对应坐标值
2021-06-05 18:21:28
使用Python、TensorFlow和Keras来进行垃圾分类的操作方法
2021-08-31 23:45:13
python获取对象信息的实例详解
2022-04-30 14:55:50
设计较好付款流程的12个建议
2009-06-08 12:45:00
![](https://img.aspxhome.com/file/UploadPic/20096/8/11-95s.png)
ACCESS的参数化查询 附ASP和C#(ASP.NET)函数
2008-01-10 12:18:00
如何防止未经注册的用户绕过注册界面直接进入应用系统?
2009-11-22 19:22:00
SQL Server转换为XQuery及反向转换
2009-01-20 13:32:00
Python魔术方法专题
2023-03-14 15:38:02
pytorch--之halfTensor的使用详解
2021-08-18 14:44:08
![](https://img.aspxhome.com/file/2023/4/91964_0s.png)
利用一个简单的例子窥探CPython内核的运行机制
2023-08-11 04:54:31
何时将数据装载到Application 或 Session 对象中去?
2009-12-03 20:17:00
Python常用模块logging——日志输出功能(示例代码)
2022-01-21 15:24:01
手把手教你pycharm专业版安装破解教程(linux版)
2023-03-03 08:58:56
![](https://img.aspxhome.com/file/2023/3/97513_0s.png)
JS设计模式之责任链模式实例详解
2023-03-17 08:23:42
![](https://img.aspxhome.com/file/2023/9/110559_0s.png)
python+ffmpeg视频并发直播压力测试
2023-09-02 10:30:33
Django后台admin的使用详解
2023-11-21 14:57:50
![](https://img.aspxhome.com/file/2023/2/83942_0s.png)
pandas库中 DataFrame的用法小结
2021-08-11 00:54:01
![](https://img.aspxhome.com/file/2023/4/87894_0s.png)
python正则-re的用法详解
2022-05-19 07:50:15
python使用Plotly绘图工具绘制散点图、线形图
2022-06-24 08:11:50
![](https://img.aspxhome.com/file/2023/7/78607_0s.png)
pytorch 状态字典:state_dict使用详解
2023-01-16 11:42:52