Python:Class和SubClass - 为什么不识别子类

时间:2014-04-20 20:39:45

标签: python python-3.3

我正在尝试创建一个包含salary和bonus属性的类,另一个包含name和idnum属性的类。通过一个小程序,询问班次是否符合当年的目标,然后计算班轮班主任的总收入。我每次尝试都会得到:
  文件“C:\ Python33 \ 12-2.py”,第53行,在main中     shift1 = Shiftsupervisor.Employee('28000.0','2240.0','Ian McGregor','S10001')     AttributeError:类型对象'Shiftsupervisor'没有属性'Employee' 我做错了什么?

# This creates two classes - ShiftSupervisor & Employee
# The program then tells us what the annual income is

# This creates a class of Super Class Shiftsupervisor which contains salary, & bonus \ 
    figures
class Shiftsupervisor:
#Initialize the Shiftsupervisor attributes
    def __init__(self, salary, bonus):
        self.__salary = salary
        self.__bonus = bonus

# creates the mutator for the attributes
    def set_salary(self, salary):
        self.__salary = salary
    def set_bonus(self, bonus):
        self.__bonus = bonus

# returns the attributes
    def get_salary(self):
        return self.__salary
    def get_bonus(self):
        return self.__bonus

#Create the subclass of employee which holds the name & idnum
#Initialize the employee attributes
class Employee(Shiftsupervisor):
    def __init__(self, salary, bonus, name, idnum):
        Shiftsupervisor.__init__(self, salary, bonus)

#Initialize the employee new attributes
        self.__name = name
        self.__idnum = idnum

#creates the new mutator for name & id
    def set_name(self, name):
        self.__name = name
    def set_idnum(self, idnum):
        self.__idnum = idnum

# new method returns the name & id
    def get_name(self):
        return self.__name
    def get_idnum(self):
        return self.__idnum

#This program take info from the two classes and gives
# the total income for the Shift Supervisor

#Creates the shift supervisor objects
def main():
    shift1 = Shiftsupervisor.Employee('28000.0','2240.0','Ian McGregor', 'S10001' )
    shift2 = Shiftsupervisor.Employee('29500','2360.0','Brian Bory', 'S20202' )
    shift3 = Shiftsupervisor.Employee('28750.0','2300.0''Finn McCool', 'S30045' )

def total_income():
    if production == 'y' or 'Y':
        return __salary + __bonus
    else:
        return __salary

#Ask the Question - Did they make production quota
    production = input('Did Shift 1 make quota this year? Type Y for yes ' )
#Print the income
    print(shift1.get_name(),'s Total income is: $', format(total_income, \
        ',.2f'), sep='')

#Ask the Question - Did they make production quota
    production = input('Did Shift 2 make quota this year? Type Y for yes ' )
#Print the income
    print(shift2.get_name(),'s Total income is: $', format(total_income, \
        ',.2f'), sep='')

#Ask the Question - Did they make production quota
    production = input('Did Shift 3 make quota this year? Type Y for yes ' )
#Print the income
    print(super3.get_name(),'s Total income is: $', format(total_income, \
        ',.2f'), sep='')

#call the main function
main()

1 个答案:

答案 0 :(得分:2)

您的代码存在以下问题:

  1. 我认为将ShiftSupervisor作为Employee的子类会更好。除非我有误解,否则轮班主管是一种员工,所以员工就是基层。轮班主管可能具有专门用于Employee类的其他属性。我添加了shift_number属性来演示此内容。
  2. 您的主要方法仅创建员工,但不会对他们做任何事情。
  3. 您的total_income方法有点困惑。请记住,__salary__bonus是对象的属性。您必须始终使用instance.attribute表单才能访问这些表单。
  4. 您不需要Python中的getter和setter。惯例是保持它们可以公开访问的正常字段,如果事实证明你需要更复杂的访问逻辑,则使用properties
  5. 工资和奖金不应该是字符串 - 它们是数字值。
  6. 总而言之,您的新代码可能如下所示:

    class Employee:
        def __init__(self, salary, bonus, name, idnum):
            self.salary = salary
            self.bonus = bonus
            self.name = name
            self.idnum = idnum
    
    class ShiftSupervisor(Employee):
        def __init__(self, salary, bonus, name, idnum, shift_number):
            super().__init__(salary, bonus, name, idnum)
            self.shift_number = shift_number
    
    
    def main():
        shift1 = ShiftSupervisor(28000.0, 2240.0, 'Ian McGregor', 'S10001', 1)
        shift2 = ShiftSupervisor(29500, 2360.0, 'Brian Bory', 'S20202', 2)
        shift3 = ShiftSupervisor(28750.0, 2300.0, 'Finn McCool', 'S30045', 3)
    
        find_income(shift1)
        find_income(shift2)
        find_income(shift3)
    
    def find_income(employee):
        production = input('Did shift {0} make quota this year? Type Y for yes '.format(employee.shift_number))
        if production.lower() == 'y':
            total_income = employee.salary + employee.bonus
        else:
            total_income = employee.salary
        print("{0}'s Total income is: ${1}".format(employee.name, total_income))
    
    main()
    

    我也感觉到你正在以某种方式纠缠一个转变和一个你不应该做的员工,尽管我完全能够把手指放在上面。一个班次可能有一个以上的员工,一个员工可以多班,但这肯定取决于你想要解决的问题。