类例2

    xiaoxiao2021-04-19  173

    class person: def __init__(self,name,job=None,pay=0): self.name=name self.job=job self.pay=pay def lastName(self): return self.name.split()[-1] def giveRaise(self,percent): #return self.pay*(1+percent) self.pay=int(self.pay*(1+percent)) def __str__(self): return 'person:name="%s",job=%s,pay=%s'%(self.name,self.job,self.pay) class manager(person): def __init__(self,name,pay): person.__init__(self,name,'mgr',pay) def giveRaise(self,percent,bonus=0.1): person.giveRaise(self,percent+bonus) if __name__=='__main__': bob=person("bob smith") sue=person("sue jhones",job='dev',pay=10000) print(bob.lastName()) #print(sue.giveRaise(0.1)) #print(sue) tom=manager('tom jones',20000) #tom.giveRaise(0.1) #print(tom) for obj in(bob,sue,tom): obj.giveRaise(0.1) print(obj)
    转载请注明原文地址: https://ju.6miu.com/read-676218.html

    最新回复(0)