本文實例講述了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 += valmyPerson = Person('Raja', 'I', 'Kumar', 21)print(myPerson)myPerson.changeAge(5)print(myPerson)print(myPerson.initials())
運行結果如下:
Raja I Kumar 21Raja I Kumar 26RIK
希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答