员工类 - Python

时间:2017-04-01 18:40:43

标签: python class

编写一个名为Employee的类,该类包含有关属性中员工的以下数据:名称,ID号,部门和职称。

编写完类后,编写一个程序,创建三个Employee对象来保存以下数据:

Susan Meyers - 47899 - 会计 - 副总裁 Mark Jones - 39119 - IT - 程序员 Joy Rogers - 81774 - 制造 - 工程师

^这是我坚持的问题。我相信我的代码的第一部分是正确的,但我不明白如何为程序的第二部分创建Employee对象。

到目前为止,这是我的代码......

emp.py文件

#create a class named Employee
class Employee:

    #initialize the attributes
    def __init__(self, name, id, department, title):
        self.__name = name
        self.__id = id
        self.__department = department
        self.__title = title

    #set the attributes
    def set_name(self, name):
        self.__name = name

    def set_id(self, id):
        self.__id = id

    def set_department(self, department):
        self.__department = department

    def set_title(self, title):
        self.__title = title

    #return the attributes
    def get_name(self):
        return self.__name

    def get_id(self):
        return self.__id

    def get_department(self):
        return self.__department

    def get_title(self):
        return self.__title

    #return the objects state as a string

    def __str__(self):
        return 'Name: ' + self.__name + \
               '\nID number: ' + self.__id + \
               '\nDepartment: ' + self.__department + \
               '\nTitle: ' + self.__title

这是我的主要功能,第二部分将要去......

 import emp


def main():
    #Create three employee objects
    emp1 = emp.Employee('name', 'id', 'department', 'title')
    emp2 = emp.Employee('name', 'id', 'department', 'title')
    emp3 = emp.Employee('name', 'id', 'department', 'title')

    #create three Employee objects for each attribute
    emp1.set_name('Susan Meyers')
    emp1.set_id('47899')
    emp1.set_department('Accounting')
    emp1.set_title('Vice President')

    emp2.set_name('Mark Jones')
    emp2.set_id('39119')
    emp2.set_department('IT')
    emp2.set_title('Programmer')

    emp3.set_name('Joy Rogersr')
    emp3.set_id('81774')
    emp3.set_department('Manufacturing')
    emp3.set_title('Engineer')

    print()
    print(emp1)
    print()
    print(emp2)
    print()
    print(emp3)


main()

所以使用这段代码,我现在得到了正确答案:

Employee 1:
Name: Susan Meyers
ID number: 47899
Department: Accounting
Title: Vice President

Employee 2:
Name: Mark Jones
ID number: 39119
Department: IT
Title: Programmer

Employee 3:
Name: Joy Rogers
ID number: 81774
Title: Programmer

Process finished with exit code 0

3 个答案:

答案 0 :(得分:0)

查看this

你已经篡改了python文件emp.py,其中包含带有import emp语句的“Employee”类。 现在,

emp1=emp.Employee('kee',1,'CSE','SE')
print emp1

将创建对象并将其指针存储到emp1。

答案 1 :(得分:0)

导入emp.py

    from emp import Employee

继续创建3个Employee对象:

    def main():
        emp1 = Employee("name", id, department, title)
        emp2 = Employee("name", id, department, title)
        emp3 = Employee("name", id, department, title)

答案 2 :(得分:0)

emp.py文件可以是:

    class Employee:

    def __init__(self, name, employee_id, department, title):
        self.name = name
        self.employee_id = employee_id
        self.department = department
        self.title = title

    def __str__(self):
        return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)

几点说明:

  • id是一个内置函数,它返回对象的“标识”。最好不要将它作为属性使用,它可能会使其他程序员感到困惑。
  • 属性不应该有双重下划线,也就是dunders。
  • 在Python中,不需要setter和getter。
  • 为格式正确的打印添加 str 方法

然后第二个是:

from emp import Employee

def main():

    # Create three employee objects
    emp1 = Employee(name='Susan Meyers', employee_id='47899', department='Accounting', title='Vice President')
    emp2 = Employee(name='Mark Jones', employee_id='39119', department='IT', title='Programmer')
    emp3 = Employee(name='Joy Rogersr', employee_id='81774', department='Manufacturing', title='Engineer')

    print(emp1, sep='/n/n')
    print(emp2, sep='/n/n')
    print(emp3, sep='/n/n')

构建时可以设置实例属性。这样可以节省许多代码。