2017-04-01 28 views
0

は、属性の従業員について、次のデータを保持しているEmployeeという名前のクラスを記述します。従業員クラスのPython

次のデータを保持するために3つのEmployeeオブジェクトを作成するプログラムを作成し、クラスを書いていたら:

スーザン・マイヤーズ - 47899 - 会計 - 副社長 マーク・ジョーンズ - 39119 - IT - プログラマー ジョイ・ロジャース - 81774 - 製造業は -

^これは私がこだわって問題となっているエンジニア。私は正しいコードの最初の部分を持っていると信じていますが、プログラムの2番目の部分でEmployeeオブジェクトを作成するために何をすべきか分かりません。ここで

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() 
...これまでのところ...

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 

、ここでは、第二部が行くために起こっている私の主な機能である私のコードです

このコードでは、今私はこの正しい答えを得ています:

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 
+2

'susan = emp.Employee( 'Susan Meyers'、47899、 'Accounting'、 '副社長') – Wright

+0

2番目の部分にコードがないことは知っています。それは私がどのように続行するか分からなかったからです。ライト、それはクラスのためのオブジェクトを作成しているのですか? – Classicalclown

+0

Googleはあなたの友人です。 "Pythonでクラスの新しいインスタンスを作成する"のようなものを入力してください...数十のチュートリアルが表示されます。たとえば、[this](https://www.dotnetperls.com/class-python) –

答えて

0

あなたはあなたのimport empステートメントを使用して、クラスの従業員」を持っているのpythonファイルemp.pyをinporedいるthis

を見てみましょう。 さて、

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

は、オブジェクトを作成し、EMP1へのポインタを格納します。

+0

ええと、私は各従業員のためにこれを行い、emp.Employeeはインポートされたファイルとクラスを正しく使用していますか?つまり、データを保持するEmployeeオブジェクトを作成するのですか? – Classicalclown

+0

うん!あなたが正しいです。 –

+0

おしゃれなクール!勉強中です! – Classicalclown

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) 
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は、オブジェクトの「同一性」を返す組み込み関数です。それを属性として使用するのは最善であり、他のプログラマーを混乱させる可能性があります。
  • アトリビュートにはダンダーという二重のアンダースコアは使用できません。
  • Pythonでは、セッターやゲッターは必要ありません。

を印刷するだけでなく、フォーマットされたため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') 

インスタンスを作成するときに設定することができます属性。これにより、多くのコード行が節約されます。

関連する問題