Skip to content

Commit 50dda64

Browse files
author
Vimal
committed
* Updated 37-method-overloading-1.py
1 parent 4474b73 commit 50dda64

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

37-method-overloading-1.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@
66
# http://shop.oreilly.com/product/0636920040057.do
77
# Chapter 24 : Method Overloading - Extending and Providing
88

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()
1818
# method in the parent.
1919

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()
2222

2323

2424
import abc
2525

2626

27-
class GetSetParent(object):
27+
class MyClass(object):
2828

2929
__metaclass__ = abc.ABCMeta
3030

31-
def __init__(self, value):
32-
self.val = 0
31+
def my_set_val(self, value):
32+
self.value = value
3333

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
3936

4037
@abc.abstractmethod
41-
def showdoc(self):
38+
def print_doc(self):
4239
return
4340

4441

45-
class GetSetInt(GetSetParent):
42+
class MyChildClass(MyClass):
4643

47-
def set_val(self, value):
44+
def my_set_val(self, value):
4845
if not isinstance(value, int):
4946
value = 0
50-
super(GetSetInt, self).set_val(self)
47+
super(MyChildClass, self).my_set_val(self)
5148

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")
5551

52+
my_instance = MyChildClass()
53+
my_instance.my_set_val(100)
54+
print(my_instance.my_get_val())
55+
print(my_instance.print_doc())
5656

0 commit comments

Comments
 (0)