为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哆啦的时光机 2021-11-23 20:09:30
我试图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)
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

我试过下面的代码。


我将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)


查看完整回答
反对 回复 2021-11-23
?
繁星coding

TA贡献1797条经验 获得超4个赞

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)


查看完整回答
反对 回复 2021-11-23
  • 2 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信