[Python #11] create e44.py
This commit is contained in:
parent
7a87bdc8e6
commit
3c7bc7cd1a
6 changed files with 116 additions and 0 deletions
13
kjg/python-the-hard-way/e44a.py
Normal file
13
kjg/python-the-hard-way/e44a.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Parent(object):
|
||||
def implicit(self):
|
||||
print("PARENT implicit()")
|
||||
|
||||
|
||||
class Child(Parent):
|
||||
pass
|
||||
|
||||
|
||||
dad = Parent()
|
||||
son = Child()
|
||||
dad.implicit()
|
||||
son.implicit()
|
14
kjg/python-the-hard-way/e44b.py
Normal file
14
kjg/python-the-hard-way/e44b.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
class Parent(object):
|
||||
def override(self):
|
||||
print("PARENT override()")
|
||||
|
||||
|
||||
class Child(Parent):
|
||||
def override(self):
|
||||
print("CHILD override()")
|
||||
|
||||
|
||||
dad = Parent()
|
||||
son = Child()
|
||||
dad.override()
|
||||
son.override()
|
16
kjg/python-the-hard-way/e44c.py
Normal file
16
kjg/python-the-hard-way/e44c.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
class Parent(object):
|
||||
def altered(self):
|
||||
print("PARENT altered()")
|
||||
|
||||
|
||||
class Child(Parent):
|
||||
def altered(self):
|
||||
print("CHILD, BEFORE PARENT altered()")
|
||||
super(Child, self).altered()
|
||||
print("CHILD, AFTER PARENT altered()")
|
||||
|
||||
|
||||
dad = Parent()
|
||||
son = Child()
|
||||
dad.altered()
|
||||
son.altered()
|
34
kjg/python-the-hard-way/e44d.py
Normal file
34
kjg/python-the-hard-way/e44d.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
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 override(self):
|
||||
print("CHILD, override()")
|
||||
|
||||
def altered(self):
|
||||
print("CHILD, BERFORE 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()
|
32
kjg/python-the-hard-way/e44e.py
Normal file
32
kjg/python-the-hard-way/e44e.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
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()
|
|
@ -147,5 +147,12 @@ How to use dictionary
|
|||
How to use class
|
||||
**** ex41
|
||||
practice class
|
||||
*** Python #9
|
||||
**** ex42
|
||||
review class and object
|
||||
*** Python #10
|
||||
**** ex43
|
||||
Analysis and Design object
|
||||
*** Python #11
|
||||
**** ex44
|
||||
difference with inheritance and compositon
|
||||
|
|
Loading…
Reference in a new issue