-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassemployee.py
More file actions
166 lines (132 loc) · 4.12 KB
/
classemployee.py
File metadata and controls
166 lines (132 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
class Employee:
raise_amount=1.04
no_of_emps=0
def __init__(self,first,last,gender,pay):
self.first=first
self.last=last
self.pay=pay
self.gender=gender
# self.fullname='%s %s' % (self.first,self.last)
Employee.no_of_emps+=1
@property
def email(self):
return '%s.%s@compant.com' % (self.first,self.last)
@property
def fullname(self):
return '%s %s' % (self.first,self.last)
@fullname.setter
def fullname(self,name):
first,last=name.split(" ")
self.first=first
self.last=last
@fullname.deleter
def fullname(self):
print("%s is deleted" % self.fullname)
self.first=None
self.last=None
def apply_raise(self):
self.pay=int(self.pay * self.raise_amount)
@classmethod
def set_raise_amount (cls,amount):
cls.raise_amount= amount
@classmethod
def from_string(cls,string):
first,last,gender,pay = string.split('-')
return cls(first,last,gender,pay)
@staticmethod
def is_workday(day):
if day.weekday() == 5 or day.weekday() == 6:
return False
return True
def __repr__(self):
return "Employee('%s','%s','%s',%s)" % (self.first,self.last,self.gender,self.pay)
def __str__(self):
return "%s %s" % (self.first,self.last)
def __add__(self,other):
return self.pay + other.pay
def __len__(self):
return len(self.fullname())
class Developer(Employee):
raise_amount=1.10
def __init__(self,first,last,gender,pay,prog_lang):
super().__init__(first,last,gender,pay)
self.prog_lang=prog_lang
class Manager(Employee):
def __init__(self,first,last,gender,pay,employees=None):
super().__init__(first,last,gender,pay)
if employees == None:
self.employees=[]
else:
self.employees=employees
def add_emp (self,emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp (self,emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps (self):
for emps in self.employees:
print("-->",emps.fullname())
#print(Employee.no_of_emps)
#emp_1= Employee('Dan','Xie','male',10000)
#emp_2= Employee('Tony','Chen','male',12000)
dev_1= Developer('Dan','Xie','male',10000,'Python')
dev_2= Developer('Tony','Chen','male',12000,'Shell')
#print(dev_1)
#print(dev_1 + dev_2)
#print(len('test'))
#print('test'.__len__())
#print(dev_1.__len__())
#print(int.__add__(1,2))
#print(str.__add__('a','b'))
#print (dev_1.pay)
#dev_1.apply_raise()
#print (dev_1.pay)
#print (dev_1.prog_lang)
mrg_1=Manager('Shawn','He','male',20000,[dev_1])
mrg_2=Manager('Shawn','Liu','male',20000)
#print(help(Developer))
#print(isinstance(mrg_1,Manager))
#print(issubclass(Manager,Employee))
#mrg_2.add_emp(dev_2)
mrg_1.remove_emp(dev_1)
#mrg_1.print_emps()
mrg_2.print_emps()
dev_1.first='Pit'
print(dev_1.first)
print(dev_1.email)
print(dev_1.fullname)
dev_1.fullname="Zhang San"
print(dev_1.fullname)
del dev_1.fullname
print(dev_1.fullname)
#print(Employee.no_of_emps)
#print(emp_1.email)
#print(emp_1.fullname())
#print (emp_1.pay)
#Employee.set_raise_amount(1.05)
#print(emp_1.raise_amount)
#print(emp_2.raise_amount)
#print(Employee.raise_amount)
#print(Employee.__dict__)
#print(emp_1.__dict__)
#print (emp_1.pay)
emp_str_1 = 'John-T-male-30000'
emp_str_2 = 'Teddy-D-male-40000'
emp_str_3 = 'Pit-R-male-50000'
emp_str_1= Employee.from_string(emp_str_1)
#print(emp_str_1.first,emp_str_1.pay)
import datetime
my_date= datetime.date(2017,3,5)
#print (Employee.is_workday(my_date))
#self: an instance
#init: an instance method (constructor) receive first argument as an instance,
#first: another argument (attribute)
#self.first: instance varible
# raise_amount: a class varible
# set_raise_amount: an class method
# a class method can work as a alternative constructor
# from_string: alternative constructor
# is_workday: a staticmethod (have a logical connection with the class)
# @property define a method and access it as a attribute