python函数重载

和C++/Objective-c/Java类似。

适用场景

父类的方法不能满足你的需求.

代码示例

 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

#!/usr/bin/python

class QBaseClass(object):
	"""QBaseClass class"""
	def __init__(self, name):
		super(QBaseClass, self).__init__()
		self.name = name

	def showMessage(self):
		print "className = %s"  % (QBaseClass.__name__)



class QPerson(QBaseClass):
	"""QPerson class"""
	def __init__(self, name,address):
		super(QPerson, self).__init__(name)
		self.address = address

	def showMessage(self):
		print "className = %s, address = %s "  % (QPerson.__name__, self.address)




p1 = QPerson("ksnowlv","北京市朝阳区")
p1.showMessage()
#下面是调用p1父类的同名方法,看起来是不是和Objective-c类似
super(QPerson,p1).showMessage()
QBaseClass.showMessage(p1)

执行情况

1
2
3
4
    ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python overloadmethod.py 
    className = QPerson, address = 北京市朝阳区
    className = QBaseClass
    className = QBaseClass

下面两种写法虽然执行结果相同,那有什么不同呢?

1
2
3

super(QPerson,p1).showMessage()
QBaseClass.showMessage(p1)