gpt4 book ai didi

Python错误: object has no attribute

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:06 25 4
gpt4 key购买 nike

我是编码新手——正在学习 Python 大学类(class)。我知道这对你们很多人来说都是显而易见的,但我不明白为什么我继续收到此错误属性错误:

prodworker = employee.ProductionWorker(shift_num, pay_rate)
AttributeError: 'Employee' object has no attribute 'ProductionWorker'

非常感谢任何帮助:)

class Employee: #writing new class called Employee:

def __init__(self, name, number): #accepts arguments for employee name and
self.__name = name #employee number
self.__number = number

def set_name(self, name): #mutator methods to set name and number
self.__name = name



def set_number(self, number):
self.__number = number


#accessor methods returns name and number
def get_name(self):
return self.__name

def get_number(self):
return self.__number



class ProductionWorker(Employee): #write subclass



def __init__(self, shift_num, pay_rate):
Employee.__init__(self, 'ProductionWorker')
self.__shift_num = shift_num
self.__pay_rate = pay_rate

def set_shift_num(self, shift_num):
self.__shift_num = shift_num

def set_pay_rate(self, pay_rate):
self.__pay_rate = pay_rate

def get_shift_num(self):
return self.__shift_num

def get_pay_rate(self):
return self.__pay_rate



#This program creates an instance of Employee Class
#and an instance of Production Worker Class:

again = 'Y'
while again.upper() == 'Y':
print('Enter the following data for the employee: \n')
name = input('What is the employee name?: ')
number = input('What is the employee number? ')


shift_num = input('What is the employee shift number? 1 = Day, 2 = Night :')
while shift_num != '1' and shift_num != '2':
shift_num = input('Invalid entry! Enter 1 for Day shift or 2 for Night shift!')
else:
if shift_num == '1':
shift_num = 'Day'
if shift_num == '2':
shift_num = 'Night'

pay_rate = float(input('What is the employee pay rate? '))

print()
print('This is an instance of the Employee class:')
employee = Employee(name, number)
print('EMPLOYEE: \t\t'+ employee.get_name())
print('EMPLOYEE NUMBER: \t' + employee.get_number())
print()

print('This is an instance of the Production Worker class: ')
prodworker = employee.ProductionWorker(shift_num, pay_rate)

print('SHIFT: \t\t\t' + ProductionWorker.get_shift_num())
print('PAY RATE: \t\t$' + format(ProductionWorker.get_pay_rate(), ',.2f'), sep='')
print('--------------------------------------------')

again = input('Enter Y to add another: ')
if again.upper() != 'Y':
print('Program End')

最佳答案

ProductionWorker 类是 Employee 类的子类,但这并不意味着您可以通过 Employee 的实例调用它。它仍然是一个顶级类,您应该直接调用。尝试将 employee.ProductionWorker(...) 替换为 ProductionWorker(...)

您将克服当前错误,但可能会遇到新错误。例如,我认为当前从 ProductionWorker.__init__ 调用 Employee.__init__ 的尝试将会失败,因为它没有传递正确数量的参数。如果您希望 employee.ProductionWorker 创建一个以某种方式与 employee 对象相关的 ProductionWorker 实例,那么您也可能会遇到逻辑问题。

我也不鼓励您使用 __double_leading_underscore 名称作为属性。这会调用 Python 的名称修改系统,该系统主要是为了帮助防止在大型或不可预测的继承层次结构中意外重用来自不同类的相同名称。如果您只想将属性设置为“私有(private)”,请使用单个下划线。这并不能保护它们不被外部代码访问,但它可以作为它们不属于对象公共(public) API 的一部分的文档。 Python 的设计理念之一是程序员对自己的行为负责(通常用“我们都是同意的成年人”这句话来描述)。如果程序员想要访问对象的私有(private)数据,他们可以这样做(这对于调试非常有用)。但如果他们弄坏了东西,除了他们自己之外,没有人可以责怪他们。

关于Python错误: object has no attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47208889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com