Tki*_*ter 3 python class python-2.x
我应该编写一个名为Employee的类,它包含名称,工资和服务年限的数据.它计算员工在退休时将获得的每月养老金支出,并显示Employee对象的三个实例变量.它还调用Employee对象的养老金方法来计算该员工的每月养老金支出并显示它.
运行我的程序后我没有得到任何输出,这是具体问题.
我应该得到这样的东西作为输出:
Name: Joe Chen
Salary: 80000
Years of service: 30
Monthly pension payout: 3600.0
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
这是完整的代码.
class Employee:
# init method implementation
def __init__(self, EmpName, EmpSalary, EmpYos):
self.EmpName = EmpName
self.EmpSalary = EmpSalary
self.EmpYos = EmpYos
def displayEmployeeDetails(self):
print "\nName ", self.EmpName,
# defines the salary details
def displaySalary(self):
print "\nSalary", self.EmpSalary
# defines the years of service
def displayYoservice(self):
print "\nYears of Service", self.EmpYos
# defines pension
def MonthlypensionPayout(self):
print "\nMonthly Pension Payout:", self.EmpSalary * self.EmpYos * 0.0015
def main():
# creates instance for employee 1
Emplo1 = Employee("Joe Chen", 80000, 30)
# creates instance for employee 2
Emplo2 = Employee("Jean park", 60000, 25)
# Function calls
Emplo1.displayEmployeeDetails()
Emplo1.displaySalary()
Emplo1.displayYoservice()
Emplo1.MonthlypensionPayout()
# function calls
Emplo2.displayEmployeeDetails()
Emplo2.displaySalary()
Emplo2.displayYoservice()
Emplo2.MonthlypensionPayout()
main()
Run Code Online (Sandbox Code Playgroud)
你只是打印一个空行.将您的功能更改为以下内容:
def displayEmployeeDetails(self):
print \
"\nName ", self.EmpName
# defines the salary details
def displaySalary(self):
print \
"\nSalary", self.EmpSalary
# defines the years of service
def displayYoservice(self):
print \
"\nYears of Service", self.EmpYos
Run Code Online (Sandbox Code Playgroud)
在\Python中是行继续.更好的是将所有这些放在同一条线上,如下所示:
def displayEmployeeDetails(self):
print "\nName ", self.EmpName
# defines the salary details
def displaySalary(self):
print "\nSalary", self.EmpSalary
# defines the years of service
def displayYoservice(self):
print "\nYears of Service", self.EmpYos
Run Code Online (Sandbox Code Playgroud)
看演示.