-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathInheritance.py
More file actions
37 lines (30 loc) · 1.59 KB
/
Copy pathInheritance.py
File metadata and controls
37 lines (30 loc) · 1.59 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
class Parent:
def __init__(self, first_name: str, last_name: str):
self.first_name = first_name
self.last_name = last_name
def get_name(self) -> str:
return f"{self.first_name} {self.last_name}"
class Child(Parent):
def __init__(self, first_name: str, last_name: str):
super().__init__(first_name, last_name)
self.previous_last_names = []
def change_last_name(self, last_name) -> None:
self.previous_last_names.append(self.last_name)
self.last_name = last_name
def get_full_name(self) -> str:
suffix = ""
if len(self.previous_last_names) > 0:
suffix = f" (née {self.previous_last_names[0]})"
return f"{self.first_name} {self.last_name}{suffix}"
person1 = Child("Elizaveta", "Alekseeva")
print(person1.get_name()) # Should print Elizaveta Actual: Elizaveta Alekseeva
print(person1.get_full_name()) # Should print Elizaveta Alekseeva
person1.change_last_name("Tyurina")
print(person1.get_name()) # Should print Elizaveta Actual: Elizaveta Tyurina
print(person1.get_full_name()) # Should print Elizaveta Alekseeva (née Tyurina) Actual: Elizaveta Tyurina (née Alekseeva)
person2 = Parent("Elizaveta", "Alekseeva")
print(person2.get_name()) # Should print Elizaveta Alekseeva
# print(person2.get_full_name()) # Method doesn't exist because only Child class has it.
# person2.change_last_name("Tyurina") # Method doesn't exist because only Child class has it.
print(person2.get_name()) # Should print Elizaveta Alekseeva
# print(person2.get_full_name()) # Method doesn't exist because only Child class has it.