-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21 - inheritance.py
More file actions
81 lines (51 loc) · 1.97 KB
/
21 - inheritance.py
File metadata and controls
81 lines (51 loc) · 1.97 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
# Python inheritance
"""Inheritance allows us to define a class that inherits all the methods and properties
from another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class"""
# Create a parent class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
# Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
# Create a child class
class Student(Person):
pass
# Example
x = Student("Mike", "Olsen")
x.printname()
"""When you add the __init__() function, the child class will no longer inherit
the parent's __init__() function.
To keep the inheritance of the parent's __init__() function, add a call to the
parent's __init__() function:
"""
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
# Using the super() function
"""Python also has a super() function that will make the child class inherit
all the methods and properties from its parent"""
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
# Add properties
"""In the example below, the year 2019 should be a variable, and passed into the Student class
when creating student objects. To do so, add another parameter in the __init__() function"""
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Mike", "Olsen", 2019)
# Add methods
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname,
"to the class of", self.graduationyear)