python自定义类并使用的演示示例,python类并,class Person
文章由Byrx.net分享于2019-03-23 10:03:13
python自定义类并使用的演示示例,python类并,class Person
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())
评论关闭