且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python 3.7的类中使用另一个实例变量从一个方法添加参数?

更新时间:2023-11-24 10:18:58

试试这个:

Try this:
class Information:
    def __init__ (self,first,last,pay):
        self.first = first
        self.last = last
        self.pay = pay

    def __str__(self):
        return self.first + " " + self.last + " will get " + str(self.pay)

    def extra(self, value):
        print("adding bonus of ", value)
        self.pay += value

emp1 = Information("tom","jerry",555666)
print(emp1)
emp1.extra(111)
print(emp1)