Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit50dda64

Browse files
author
Vimal
committed
* Updated 37-method-overloading-1.py
1 parent4474b73 commit50dda64

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 theChild class.
11-
12-
# 1) We have defined `GetSetParent()` as an abstract class,
13-
# and it has three methods,set_val(),get_val(), andshowdoc().
14-
# 2)GetSetInt() inherits fromGetSetParent()
15-
# 3)GetSetInt() extends the parent'sset_val() method
16-
# by it's ownset_val() method. It checks for the input,
17-
# checks if it's an integer, and then calls theset_val()
9+
# This code is an example on how we can extend a method inherited by
10+
#a child class from theParent class.
11+
12+
# 1) We have defined `MyClass()` as an abstract class,
13+
# and it has three methods,my_set_val(),my_get_val(), andprint_doc().
14+
# 2)MyChildClass() inherits fromMyClass()
15+
# 3)MyChildClass() extends the parent'smy_set_val() method
16+
# by it's ownmy_set_val() method. It checks for the input,
17+
# checks if it's an integer, and then calls themy_set_val()
1818
# method in the parent.
1919

20-
# 4) Theshowdoc() method in the Parent is an abstract method
21-
# and hence should be implemented in the child classGetSetInt()
20+
# 4) Theprint_doc() method in the Parent is an abstract method
21+
# and hence should be implemented in the child classMyChildClass()
2222

2323

2424
importabc
2525

2626

27-
classGetSetParent(object):
27+
classMyClass(object):
2828

2929
__metaclass__=abc.ABCMeta
3030

31-
def__init__(self,value):
32-
self.val=0
31+
defmy_set_val(self,value):
32+
self.value=value
3333

34-
defset_val(self,value):
35-
self.val=value
36-
37-
defget_val(self):
38-
returnself.val
34+
defmy_get_val(self):
35+
returnself.value
3936

4037
@abc.abstractmethod
41-
defshowdoc(self):
38+
defprint_doc(self):
4239
return
4340

4441

45-
classGetSetInt(GetSetParent):
42+
classMyChildClass(MyClass):
4643

47-
defset_val(self,value):
44+
defmy_set_val(self,value):
4845
ifnotisinstance(value,int):
4946
value=0
50-
super(GetSetInt,self).set_val(self)
47+
super(MyChildClass,self).my_set_val(self)
5148

52-
defshowdoc(self):
53-
print("GetSetInt object {0}), only accepts integer values".format(
54-
id(self)))
49+
defprint_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)

[8]ページ先頭

©2009-2025 Movatter.jp