-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphism.py
83 lines (63 loc) · 1.91 KB
/
polymorphism.py
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
"""Polymorphism example
"""
class Person:
"""Person Class
"""
def __init__(self, name: str, family: str, age: int):
self.name = name
self.family = family
self.age = age
def __str__(self):
return f"{self.name} {self.family}, {self.age} year"
def add_age(self):
"""add one year to age
"""
self.age += 1
def change_info(self, **variables):
"""change person information
Args:
name (str): person name
family (str): person family
age (int): person age
"""
self.name = variables.get("name", self.name)
self.family = variables.get("family", self.family)
self.age = variables.get("age", self.age)
class Employee(Person):
"""Employee Class
"""
def __init__(self, name: str, family: str, age: int, salary: int):
super().__init__(name, family, age)
self.salary = salary
def __str__(self):
return f"{super().__str__()}, {self.salary}$"
def add_salary(self, value: int):
"""add value to salary
"""
self.salary += value
def change_info(self, **variables):
"""change employee information
Args:
name (str): employee name
family (str): employee family
age (int): employee age
salary (int): employee salary
"""
super().change_info(**variables)
self.salary = variables.get("salary", self.salary)
# create person object
person = Person("mohammad", "dori", 17)
print(person)
person.add_age()
print(person)
person.change_info(age=20, family="sharifi")
print(person)
print("----------------------------------")
# create employee object
employee = Employee("salar", "dori", 30, 1000)
print(employee)
employee.add_age()
employee.add_salary(-100)
print(employee)
employee.change_info(salary=2000, name="mohammad", age=20)
print(employee)