2017-04-08 120 views
0

我是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) 
+2

你有什么问题Job.rateself.rate之间的区别?你是否收到错误信息(那么你应该把它粘贴在这里),或者你没有想到的结果(给我们错误的输出以及你的期望)。你的代码的缩进是错误的。所有属于该类的部分都应该缩进。即使正确缩进,你也会因错别字而受到错误('Charge'vs'charge'...)请首先纠正! –

+0

我觉得有一个错字。充电方法从Caps'C'开始。 –

回答

0

也许有点格式化和一些评论可能会有所帮助。我将charge的情况正确设置为Charge方法名称。我创建Charge2显示的方法

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 
+0

非常感谢您的解决方案。我很抱歉我的代码草率的演示文稿,我试图尽可能快地学习,并且忽略了缩进。我可以看到如何改变特定实例的类变量,以及如果我使用的输出可能会有所不同无论是课堂还是实例。 –

+0

欢迎您,如果这不包括您的问题,让我知道它没有或缺少什么信息。如果需要,您可以将此代码粘贴到http://pythonfiddle.com/并将其作为测试运行。 –