python自定义类并使用的方法,python自定义类并
python自定义类并使用的方法,python自定义类并
本文实例讲述了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查询sqlite数据表的方法,pythonsqlite
- python实现在sqlite动态创建表的方法,pythonsqlite
- python操作sqlite的CRUD实例分析,sqlitecrud实例分析
- python通过ftplib登录到ftp服务器的方法,pythonftplib
- python使用smtplib模块通过gmail实现邮件发送的方法,pyt
- Python类定义和类继承详解,python详解
- python通过imaplib模块读取gmail里邮件的方法,pythonimapli
- Python类的定义、继承及类对象使用方法简明教程,pyt
- python使用nntp读取新闻组内容的方法,pythonnntp
- python使用htmllib分析网页内容的方法,pythonhtmllib
评论关闭