|
6 | 6 | # http://shop.oreilly.com/product/0636920040057.do
|
7 | 7 | # Chapter 24 : Method Overloading - Extending and Providing
|
8 | 8 |
|
9 |
| -# This code is an example on how we can extend a method |
10 |
| -# defined in a Parent class, in the Child class. |
11 |
| - |
12 |
| -# 1) We have defined `GetSetParent()` as an abstract class, |
13 |
| -# and it has three methods, set_val(), get_val(), and showdoc(). |
14 |
| -# 2) GetSetInt() inherits from GetSetParent() |
15 |
| -# 3) GetSetInt() extends the parent's set_val() method |
16 |
| -# by it's own set_val() method. It checks for the input, |
17 |
| -# checks if it's an integer, and then calls the set_val() |
| 9 | +# This code is an example on how we can extend a method inherited by |
| 10 | +# a child class from the Parent class. |
| 11 | + |
| 12 | +# 1) We have defined `MyClass()` as an abstract class, |
| 13 | +# and it has three methods, my_set_val(), my_get_val(), and print_doc(). |
| 14 | +# 2) MyChildClass() inherits from MyClass() |
| 15 | +# 3) MyChildClass() extends the parent's my_set_val() method |
| 16 | +# by it's own my_set_val() method. It checks for the input, |
| 17 | +# checks if it's an integer, and then calls the my_set_val() |
18 | 18 | # method in the parent.
|
19 | 19 |
|
20 |
| -# 4) The showdoc() method in the Parent is an abstract method |
21 |
| -# and hence should be implemented in the child class GetSetInt() |
| 20 | +# 4) The print_doc() method in the Parent is an abstract method |
| 21 | +# and hence should be implemented in the child class MyChildClass() |
22 | 22 |
|
23 | 23 |
|
24 | 24 | import abc
|
25 | 25 |
|
26 | 26 |
|
27 |
| -class GetSetParent(object): |
| 27 | +class MyClass(object): |
28 | 28 |
|
29 | 29 | __metaclass__ = abc.ABCMeta
|
30 | 30 |
|
31 |
| - def __init__(self, value): |
32 |
| - self.val = 0 |
| 31 | + def my_set_val(self, value): |
| 32 | + self.value = value |
33 | 33 |
|
34 |
| - def set_val(self, value): |
35 |
| - self.val = value |
36 |
| - |
37 |
| - def get_val(self): |
38 |
| - return self.val |
| 34 | + def my_get_val(self): |
| 35 | + return self.value |
39 | 36 |
|
40 | 37 | @abc.abstractmethod
|
41 |
| - def showdoc(self): |
| 38 | + def print_doc(self): |
42 | 39 | return
|
43 | 40 |
|
44 | 41 |
|
45 |
| -class GetSetInt(GetSetParent): |
| 42 | +class MyChildClass(MyClass): |
46 | 43 |
|
47 |
| - def set_val(self, value): |
| 44 | + def my_set_val(self, value): |
48 | 45 | if not isinstance(value, int):
|
49 | 46 | value = 0
|
50 |
| - super(GetSetInt, self).set_val(self) |
| 47 | + super(MyChildClass, self).my_set_val(self) |
51 | 48 |
|
52 |
| - def showdoc(self): |
53 |
| - print("GetSetInt object {0}), only accepts integer values".format( |
54 |
| - id(self))) |
| 49 | + def print_doc(self): |
| 50 | + print("Documentation for MyChild Class") |
55 | 51 |
|
| 52 | +my_instance = MyChildClass() |
| 53 | +my_instance.my_set_val(100) |
| 54 | +print(my_instance.my_get_val()) |
| 55 | +print(my_instance.print_doc()) |
56 | 56 |
|
0 commit comments