-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinheritance.py
More file actions
52 lines (36 loc) · 1.14 KB
/
inheritance.py
File metadata and controls
52 lines (36 loc) · 1.14 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
# 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
x = Student("Mike", "Olsen")
x.printname()
# Add the __init__() Function
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
# Use the super() Function
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
# Add Properties
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)