从类中返回方法

时间:2017-04-08 17:47:51

标签: python

我是python和学习课程的新手。我在下面的课程中返回方法charge时遇到了困难。我尝试使用.feecharge都不起作用。

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)

1 个答案:

答案 0 :(得分:0)

也许有点格式化和一些评论可能会有所帮助。我将charge的情况正确地设置为Charge方法名称。我创建了Charge2来显示方法中Job.rateself.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