我是python和学习课程的新手。我在下面的课程中返回方法charge
时遇到了困难。我尝试使用.fee
和charge
都不起作用。
class Job:
rate = 1.04
def __init__(self, location, salary, description, fee) :
self.location = location
self.salary = salary
self.description = description
self.fee = fee
def Charge(self):
self.fee = int( self.fee + Job.rate)
job1=Job("london",23000,"Accounts Assistant",1200)
job2=Job("london",25000,"Accounts Assistant",500)
job1.rate = 1.05
job1.charge()
print(job1.fee)
答案 0 :(得分:0)
也许有点格式化和一些评论可能会有所帮助。我将charge
的情况正确地设置为Charge
方法名称。我创建了Charge2
来显示方法中Job.rate
和self.rate
之间的区别
class Job:
rate = 1.04
def __init__(self, location, salary, description, fee) :
self.location = location
self.salary = salary
self.description = description
self.fee = fee
def Charge(self):
self.fee = int( self.fee + Job.rate) # use class amount 1.04
def Charge2(self):
self.fee = int( self.fee + self.rate) # use instance (11.05 set below)
job1 = Job("london",23000,"Accounts Assistant",1200) # create instance
job2 = Job("london",25000,"Accounts Assistant",500) # create instance
job1.rate = 11.05
job1.Charge() # execute, adds 1.04 to 1200, then make int of that
print(job1.fee) # outputs 1201
job1.Charge2() # execute, adds 11.05 to 1201, then make int of that
print(job1.fee) # outputs 1212
print(job2.fee) # outputs 500
job1.Charge2() # execute, adds 11.05 to 1212, then make int of that
print(job1.fee) # outputs 1223
job1.Charge() # execute, adds 1.04 to 1223, then make int of that
print(job1.fee) # outputs 1224