0
私はPython 3でクラスを扱っています。私はそれらに苦労しています。私はここに2つのプログラムを持っています(1人は他の人にインポートされています) あなたは従業員のリストを作成しており、従業員のHourly、Salary、Volunteerという3つのクラスがあります。クラスで動作するPython
各クラスのshow_payに問題があるようです。私はまた、私の給料のクラスでは整数で文字列を分割しようとしているが、私はコードを書かれている方法では、私はそれを回避する方法がかなりわからないことを知っている。 My Hourlyクラスがリストに印刷されていないようです。
ありがとうございます。私はこれと本当に混乱しており、私はこのプロジェクトを手伝っています。
最初のプロジェクト(従業員)
#set global constant
SHIFT_2 = 0.05
SHIFT_3 = 0.10
#person class
class Person:
#initialize name, ID number, city
def __init__(self, name, ID, city):
self.__ID = ID
self.__name = name
self.__city = city
#display employee name
def show_person(self):
print('Name:', self.__name)
print('ID:', self.__ID)
print('City:', self.__city)
#display salary
def show_pay(self):
print('I make lots of money')
#return formatting
def __str__(self):
name_string = '(My name is ' + self.__name +')'
return name_string
# Hourly employee class
class Hourly(Person):
#initialize method calls superclass
def __init__(self, name, ID, city, base_pay, shift):
Person.__init__(self, name, ID, city)
self.__base_pay = base_pay
self.__shift = shift
#show_pay overrides the superclass and displays hourly pay rates
def show_pay(self):
if self.__shift == 1:
print('My salary is ', self.__base_pay)
elif self.__shift == 2:
print('My salary is ', (self.__base_pay * SHIFT_2) + self.__base_pay)
elif self.__shift == 3:
print('My salary is ', (self.__base_pay * SHIFT_3) + self.__base_pay)
#salary employee class
class Salary(Person):
#intialize method calls superclass
def __init__(self, name, ID, city, ann_salary):
Person.__init__(self, name, ID, city)
self.__salary = ann_salary
#show pay overrides superclass and displays salary pay rates
def show_pay(self):
print('I make ', self.__salary)
print('which is ', self.__salary // 26, 'every two weeks.')
#volunteer employee class
class Volunteer(Person):
def __init__(self, name, ID, city):
Person.__init__(self, name, ID, city)
def show_pay(self):
print('I am a volunteer so I am not paid.')
これは、あなたが文字列として給与を入力しているメインプログラム
import employee
def main():
#create list
employees = make_list()
#display list
print('Here are the employees.')
print('-----------------------')
display_list(employees)
def make_list():
#create list
employee_list = []
#get number of hourly employees
number_of_hourly = int(input('\nHow many hourly will be entered? '))
for hourly in range(number_of_hourly):
#get input
name, ID, city = get_input()
base_pay = input('Enter employee base pay: ')
shift = input('Enter employee shift 1,2, or 3: ')
#create object
Hourly = employee.Hourly(name, ID, city, base_pay, shift)
#add object to list
employee_list.append(Hourly)
#get number of salary employees
number_of_salary = int(input('\nHow many salary will be entered? '))
for salary in range(number_of_salary):
#get input
name, ID, city = get_input()
ann_salary = input('Enter employee annual salary: ')
#create object
salary = employee.Salary(name, ID, city, ann_salary)
#add object to list
employee_list.append(salary)
#get volunteers
number_of_volunteers = int(input('\nHow many other volunteers will be entered? '))
for volunteers in range(number_of_volunteers):
#get info
name, ID, city = get_input()
#create object
volunteer = employee.Person(name, ID, city)
#add object to list
employee_list.append(volunteer)
#invalid object
employee_list.append('\nThis is invalid')
#return employee_list
return employee_list
def get_input():
#input name
name = input("Employee's name: ")
#validate
while name == '':
print('\n Name is required. Try again.')
name = input("Employee's name: ")
ID_valid = False
ID = input("Employee's ID: ")
while ID_valid == False:
try:
ID = float(ID)
if ID > 0:
ID_valid = True
else:
print("\nID must be > 0. Try again.")
ID = input("Employee's age: ")
except ValueError:
print("\nID must be numeric. Try again.")
ID = input("Employee's ID: ")
#get city
city = input("Enter employee's city of residence: ")
#return values
return name, ID, city
def display_list(human_list):
#create for loop for isinstance
for human in human_list:
#create isinstance
if isinstance(human, employee.Person):
print(human)
human.show_person()
human.show_pay()
print
else:
print('Invalid employee object')
#call main function
main()
あなたの質問は何ですか?また、すべてのクラス変数の前に '__'をつける必要はありません。読みやすさを実現します。 – Blender
私はクラスでshow_payがその特定のクラス(Hourly、Salary、またはVolunteer)の下にあるものに変更する必要があると思いますが、何らかの理由でそれが動作しません。 __についてお詫び申し上げます。私はこれに新しく、それは私の本が言うようにそういうことなので、私は他の方法を知らない。 –
実際には、上記のコーディングがうまくいかない場合は、 '__'という接頭辞の名前が関係します。おそらく "これはPythonのプライベートプロパティーをどうやって行うのか"というところですが、それは厳密には間違っているでしょう。しかし、このようなコードはかなり簡単に誤動作させる可能性があります。ちょうどそれらを取り除く。 – jsbueno