Skip to content

Commit 1a4a9e5

Browse files
Inheritance exercise complete
1 parent 773ecb8 commit 1a4a9e5

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

prep-exercises/inheritance.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# --- Exercise ---
2+
# Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines).
3+
4+
# class Parent:
5+
# def __init__(self, first_name: str, last_name: str):
6+
# self.first_name = first_name
7+
# self.last_name = last_name
8+
9+
# def get_name(self) -> str:
10+
# return f"{self.first_name} {self.last_name}"
11+
12+
13+
# class Child(Parent):
14+
# def __init__(self, first_name: str, last_name: str):
15+
# super().__init__(first_name, last_name)
16+
# self.previous_last_names = []
17+
18+
# def change_last_name(self, last_name) -> None:
19+
# self.previous_last_names.append(self.last_name)
20+
# self.last_name = last_name
21+
22+
# def get_full_name(self) -> str:
23+
# suffix = ""
24+
# if len(self.previous_last_names) > 0:
25+
# suffix = f" (née {self.previous_last_names[0]})"
26+
# return f"{self.first_name} {self.last_name}{suffix}"
27+
28+
# person1 = Child("Elizaveta", "Alekseeva")
29+
# print(person1.get_name())
30+
# print(person1.get_full_name())
31+
# person1.change_last_name("Tyurina")
32+
# print(person1.get_name())
33+
# print(person1.get_full_name())
34+
35+
# person2 = Parent("Elizaveta", "Alekseeva")
36+
# print(person2.get_name())
37+
# print(person2.get_full_name())
38+
# person2.change_last_name("Tyurina")
39+
# print(person2.get_name())
40+
# print(person2.get_full_name())
41+
42+
# --- Solution ---
43+
44+
# This defines a class called "Parent" with an initializer that sets the first and last name, and a method to get the full name.
45+
class Parent:
46+
def __init__(self, first_name: str, last_name: str):
47+
self.first_name = first_name
48+
self.last_name = last_name
49+
50+
def get_name(self) -> str:
51+
return f"{self.first_name} {self.last_name}"
52+
53+
# This defines a class called "Child" that inherits from "Parent". It has an initializer that calls the parent's initializer and adds a list that can track previous last names. It also has methods to change the last name and get the full name with any previous last names included.
54+
class Child(Parent):
55+
def __init__(self, first_name: str, last_name: str):
56+
super().__init__(first_name, last_name)
57+
self.previous_last_names = []
58+
59+
def change_last_name(self, last_name) -> None:
60+
self.previous_last_names.append(self.last_name)
61+
self.last_name = last_name
62+
63+
def get_full_name(self) -> str:
64+
suffix = ""
65+
if len(self.previous_last_names) > 0:
66+
suffix = f" (née {self.previous_last_names[0]})"
67+
return f"{self.first_name} {self.last_name}{suffix}"
68+
69+
person1 = Child("Elizaveta", "Alekseeva")
70+
print(person1.get_name())
71+
print(person1.get_full_name())
72+
person1.change_last_name("Tyurina")
73+
print(person1.get_name())
74+
print(person1.get_full_name())
75+
76+
person2 = Parent("Elizaveta", "Alekseeva")
77+
print(person2.get_name())
78+
print(person2.get_full_name()) # This line will cause an error because the Parent class does not have a get_full_name method.
79+
person2.change_last_name("Tyurina") # This line will cause an error because the Parent class does not have a change_last_name method.
80+
print(person2.get_name())
81+
print(person2.get_full_name()) # This line will cause an error because the Parent class does not have a get_full_name method.

0 commit comments

Comments
 (0)