-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex44.inheritance-and-composition.py
74 lines (62 loc) · 1.38 KB
/
ex44.inheritance-and-composition.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
# Learn Python the Hard Way
# Exercise 44 - Inheritance and Composition
# Using Inheritance
# class Parent(object):
#
# def override(self):
# print "PARENT override()"
#
# def implicit(self):
# print "PARENT implicit()"
#
# def altered(self):
# print "PARENT altered()"
#
# class Child(Parent):
#
# def __init__(self, stuff):
# self.stuff = stuff
# super(Child, self).__init__()
#
# def override(self):
# print "CHILD override()"
#
# def altered(self):
# print "CHILD, BEFORE PARENT altered()"
# super(Child, self).altered()
# print "CHILD, AFTER PARENT altered()"
#
# dad = Parent()
# son = Child()
#
# dad.implicit()
# son.implicit()
#
# dad.override()
# son.override()
#
# dad.altered()
# son.altered()
# Avoiding Inheritance
class Other(object):
def override(self):
print "OTHER override()"
def implicit(self):
print "OTHER implicit()"
def altered(self):
print "OTHER altered()"
class Child(object):
def __init__(self):
self.other = Other()
def implicit(self):
self.other.implicit()
def override(self):
print "CHILD override()"
def altered(self):
print "CHILD, BEFORE OTHER altered()"
self.other.altered()
print "CHILD, AFTER OTHER altered()"
son = Child()
son.implicit()
son.override()
son.altered()