如何通过实例方法传递整数并将其添加到实例变量中?

我试图bonus用实例变量添加参数(它将采用整数),self.pay并想用工人的姓名打印最终付款。但是,我无法打印增加的总付款


我想调用该方法rise()而不是从中返回任何内容,但我很困惑如何调用它并传递一个整数。


class Information:

    def __init__(self,first,last,pay):


        self.first = first

        self.last = last

        self.pay = pay



    def rise(self,int(bonus)):

        self.pay = self.pay + bonus


    def __str__(self):

        return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)


if __name__ == "__main__":

    emp1 = Information("tom","jerry",999)

    print (emp1)


哆啦的时光机
浏览 140回答 2
2回答

哔哔one

我试过下面的代码。我将defrise(self,int(bonus)): 更新为defrise(self,bonus):class Information:    def __init__(self,first,last,pay):        self.first = first        self.last = last        self.pay = pay    def rise(self,bonus):        self.pay = self.pay + bonus    def __str__(self):        return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)if __name__ == "__main__":    emp1 = Information("tom","jerry",999)    emp1.rise(89)    print (emp1)

繁星coding

class Information:    def __init__(self,first,last,pay):        self.first = first        self.last = last        self.pay = pay    def raise_salary(self, bonus):        self.pay += int(bonus) # exception if bonus cannot be casted    def __str__(self):        return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)if __name__ == "__main__":    emp1 = Information("tom", "jerry", 999)    print(emp1)    emp1.raise_salary('1000') # or just emp1.raise(1000)    print(emp1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python