Python学习:类
1.类的构造方法__init__(),这个是两个下划线;
2.类包含属性(变量)和方法;
3.类可以继承
"""Employee moudle"""
class Employees():
"""class employees"""
def __init__(self, employees):
"""init employee"""
self.employee = {
'name':'', 'age': 0, 'sex': 'male',
'position': '', 'score': 0
}
self.employees = employees[:]
def input_employee(self):
"""Obtain employee information through user input"""
while True:
for key in self.employee.keys():
self.employee[key] = input("Please give the empolyee's " + key + ": ")
if self.employee[key] == '':
break
if key == 'age' or key == 'score':
self.employee[key] = int(self.employee[key])
if self.employee['name'] != '':
self.employees.append(self.employee)
end_of_input = input("Do you want to continue? (Y/n) ")
if end_of_input.lower() != 'y':
break
return self.employee
def employee_information_name(self):
"""Describe what employee information is included"""
keys = ['']
employee_num = len(self.employees)
while employee_num:
employee_num -= 1
for key_temp in self.employees[employee_num].keys():
keys_num = 0
for key in keys:
if key_temp != key:
keys_num += 1
if keys_num == len(keys):
keys.append(key_temp)
keys.pop(0)
#~ print(keys)
return keys
def employee_key_sort(self, keys):
"""Put employee position last"""
keys_num = 0
keys_len = len(keys)
for key in keys:
if key == 'position':
keys[keys_num ] = keys[keys_len -1]
keys_num += 1
keys[keys_len -1] = 'position'
return keys
def print_employee_information(self):
"""Print employee information"""
keys = self.employee_information_name()
self.employee_key_sort(keys)
message = '\n'
for key in keys:
message += key + " "
message += '\n'
employee_num = len(self.employees)
while employee_num:
employee_num -= 1
for key in keys:
if key == 'age' or key == 'score':
message += str(self.employees[employee_num][key]) + " "
else:
message += self.employees[employee_num][key] + " "
message += '\n'
print(message)
def employee_rank(self, key):
"""Rank employees based on key"""
values = []
employee_num = len(self.employees)
while employee_num:
employee_num -= 1
values.append(self.employees[employee_num][key])
values.sort(reverse = True)
value_temp = 0
for value in values:
if value == value_temp:
values.remove(value)
value_temp = value
employees_sorted = []
value_num = len(values)
while value_num:
value_num -= 1
employee_num = len(self.employees)
while employee_num:
employee_num -= 1
if self.employees[employee_num][key] == values[value_num]:
employees_sorted.append(self.employees[employee_num])
self.employees = employees_sorted[:]
return self.employees
from employee_class import Employees
employee_0 = {
'name': 'jack', 'age': 30, 'sex': 'male',
'position': 'intermediate engineer',
'score': 91
}
employee_1 = {
'name': 'nick', 'age': 25, 'sex': 'male',
'position': 'junior engineer',
'score': 85
}
employee_2 = {
'name': 'jason', 'age': 28, 'sex': 'male',
'position': 'super engineer',
'score': 95
}
employee_3 = {
'name': 'tom', 'age': 35, 'sex': 'male',
'position': 'expert',
'score': 98
}
employee_4 = {
'name': 'jacky', 'age': 33, 'sex': 'male',
'position': 'manager',
'score': 95
}
employees = [employee_0, employee_1, employee_2, employee_3, employee_4]
my_employees = Employees(employees);
my_employees.input_employee()
my_employees.employee_rank('score')
my_employees.print_employee_information()