我有以下课程:
from forwind.lidarapi.api import MCLidarGUIPlugin
class MCLidarActions( Handler ):
tcp_send = Event
def object__updated_changed( self, info ):
print info;
pass;
def _tcp_send_changed( self ):
print( "Click" )
和
from forwind.lidarapi.actions.api import MCLidarActions
class MCUDPActions( MCLidarActions ):
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click UDP" )
当我点击MCLidarActions
中的按钮时,将调用_tcp_send_changed函数,如何扩展此函数,我也想在MCUDPActions中执行操作。在这种情况下,如果我点击该按钮,它将打印出click
,但我还打印出Click UDP
答案 0 :(得分:1)
以下是调用super()的示例 请注意,为了使超级工作,你需要继承对象。
class E(object):
def __init__(self):
print "enter E"
print "leave E"
class F(E):
def __init__(self):
print "enter F"
super(F, self).__init__()
print "leave F"
f = F()
答案 1 :(得分:1)
如果我理解你的问题,你可以这样做:
class MCLidarActions( object ):
li = []
tcp_send = 'Event'
def object__updated_changed( self, info ):
print info;
pass;
def _tcp_send_changed( self ):
print( "Click" )
for x in self.li:
x._tcp_send_changed()
class MCUDPActions( MCLidarActions ):
def __init__(self):
self.li.append(self)
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click UDP" )
class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
def __init__(self):
self.li.append(self)
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click _uuuuuuuuuuuuuutp_" )
M = MCLidarActions()
print 'M, instance of MCLidarActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
a = MCUDPActions()
print '\na, instance of MCUDPActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()
b = MCUDPActions()
print '\nb, instance of MCUDPActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()
print
print ' executing b._tcp_send_changed():'
b._tcp_send_changed()
v = MC_uuuuuuuuuuuuuutp_Actions()
print '\nv, instance of MC_uuuuuuuuuuuuuutp_Actions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()
print
print ' executing b._tcp_send_changed():'
b._tcp_send_changed()
print
print ' executing v._tcp_send_changed():'
v._tcp_send_changed()
结果
M, instance of MCLidarActions, created ------------
executing M._tcp_send_changed():
Click
a, instance of MCUDPActions, created ------------
executing M._tcp_send_changed():
Click
Click UDP
executing a._tcp_send_changed():
Click UDP
b, instance of MCUDPActions, created ------------
executing M._tcp_send_changed():
Click
Click UDP
Click UDP
executing a._tcp_send_changed():
Click UDP
executing b._tcp_send_changed():
Click UDP
v, instance of MC_uuuuuuuuuuuuuutp_Actions, created ------------
executing M._tcp_send_changed():
Click
Click UDP
Click UDP
Click _uuuuuuuuuuuuuutp_
executing a._tcp_send_changed():
Click UDP
executing b._tcp_send_changed():
Click UDP
executing v._tcp_send_changed():
Click _uuuuuuuuuuuuuutp_
但是在上面的代码中,有必要在基类的每个子类 MCUDPActions 和 MC_uuuuuuuuuuuuut_Actions 中定义一个函数 __ init __ MCLidarActions 强>
为避免这种情况, li 中的追加可以放在基类中:
class MCLidarActions( object ):
li = []
tcp_send = 'Event'
def __init__(self):
if self.__class__ != MCLidarActions:
self.li.append(self)
def object__updated_changed( self, info ):
print info;
pass;
def _tcp_send_changed( self ):
print( "Click" )
for x in self.li:
x._tcp_send_changed()
class MCUDPActions( MCLidarActions ):
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click UDP" )
class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click _uuuuuuuuuuuuuutp_" )
,结果完全一样。