我有一个employee类,它是生产工人类的超类。存在这样一个问题:生产工人的实例似乎无法识别其超类(Employee)的一个已定义字段的存在。

下面是错误消息以及我输入到程序中的内容

Please enter the employee's number: 9987
Please enter the employee's name: Leo
Please enter the employee's shift number (1 for day shift 2 for night shift): 2
Please enter the employee's hourly pay rate: 18.67
-------------------
Here is some information about your employee
Traceback (most recent call last):
  File "jdoodle.py", line 46, in <module>
    main()
  File "jdoodle.py", line 40, in main
    print("Here is their employee number: " + str(person.getEmpNum()))
  File "jdoodle.py", line 12, in getEmpNum
    return __empNum
NameError: name '_Employee__empNum' is not defined

下面是类的定义

class Employee:
    def setName(self, name):
        self.__empName = name

    def setEmpNum(self, empNum):
        self.__empNum = empNum

    def getName(self):
        return __empName

    def getEmpNum(self):
        return __empNum
class ProductionWorker(Employee):
    def setShiftNum(self, num):
        self.__shiftNum = num

    def setPayRate(self, payrate):
        self.__payrate = payrate

    def getShiftNum(self):
        return __shiftNum

    def getPayRate(self):
        return __payrate

下面是主要的方法

def main():   
    person = ProductionWorker()
    empNum = int(input("Please enter the employee's number: "))
    empName = input("Please enter the employee's name: ")
    shiftNum = int(input("Please enter the employee's shift number (1 for day shift 2 for night shift): "))
    payrate = float(input("Please enter the employee's hourly pay rate: "))
    person.setName(empName)
    person.setEmpNum(empNum)
    person.setShiftNum(shiftNum)
    person.setPayRate(payrate)

    print("-------------------")
    print("Here is some information about your employee")
    print("Here is their employee number: " + str(person.getEmpNum()))
    print("Here is their name: " + person.getName())
    print("Here is their payrate: " + str(person.getPayRate()))
    print("Here is their shift number: " + str(person.getShiftNum()))
    print("-------------------")

转载请注明出处:http://www.zhongtian365.com/article/20230526/1865364.html

随机推荐

  1. python继承threading.Thread实现有返回值的子类实例

    继承与threading.Thread实现有返回值的子类MyThread,废话不多说,大家直接看代码 import threading class MyThread(threading.Thread): def __init__(...

  2. Python 之继承

    概要 如果要修改现有类的行为,我们不必再从头建一个新的类,可以直接利用继承这一功能。下面将以实例相结合介绍继承的用法。 新建一个基类 代码如下: class Marvel(object): num_presents =...

  3. python的继承知识点总结

    python继承,python丰富的类因为继承而变得多姿多彩,如果语言不支持继承,那么类就没什么优势。 1、首先我们来定义两个类 一个dog类,一个bird类class Dog: def sleeping(self): print...

  4. 继承自Python tkinter框架,搞砸了包功能

    下面是代码:import tkinter as tk def click(event): print (event.widget.winfo_name()) sq=250 win = tk.Tk() topFrame = tk...

  5. 类: Python中继承类中的__new__和__init__

    我有一个这样的类:class FloatArray(): def __init__(self): self.a = 1.5 def __new__( cls, value ): data =...

  6. python中继承的抽象类

    在python中,我可以通过继承另一个抽象类来定义一个接口(抽象类)吗?如果我尝试:import abc ABC = abc.ABCMeta(ABC, (object,), {}) class interface(ABC): @a...

  7. 如何标记继承Python时需要重载的方法?

    我想告诉未来的程序员,如果继承了AbstractCrawler,下面的类方法必须被覆盖。class AbstractCrawler(object): def get_playlist_videos(): pass ...

  8. Python 继承,重写,super()调用父类方法操作示例

    本文实例讲述了Python 继承,重写,super()调用父类方法操作。分享给大家供大家参考,具体如下: demo.py(继承,重写,super): # 父类 class Dog: def bark(self): pri...

  9. 为什么当一个子类继承/不继承Python中的第三个类时,这个类的MRO会发生变化?

    我正在尝试更好地理解Python中的MRO,我遇到了这个例子: class A: def process(self): print(A process()) class B(A): pass cla...

  10. python中子类继承父类的__init__方法实例

    前言 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的 初始化 。 注意:这个名称的开始和结尾都是双下划...