Virtual function: extend method behaviour : Class Virtual « Class « Python






Virtual function: extend method behaviour

Virtual function: extend method behaviour

class Super:
    def method(self):
        print 'in Super.method'       
    def delegate(self):
        self.action()                 

class Inheritor(Super):               
    pass

class Replacer(Super):                
    def method(self):
        print 'in Replacer.method'

class Extender(Super):                
    def method(self):
        print 'starting Extender.method'
        Super.method(self)
        print 'ending Extender.method'

class Provider(Super):                
    def action(self):
        print 'in Provider.action'

if __name__ == '__main__':
    for klass in (Inheritor, Replacer, Extender):
        print '\n' + klass.__name__ + '...'
        klass().method()

    print '\nProvider...'
    x = Provider()
    x.delegate()

           
       








Related examples in the same category

1.Override predefined functionOverride predefined function
2.Virtual function in PythonVirtual function in Python
3.Virtual function in Python classVirtual function in Python class
4.Virtual function in two base classVirtual function in two base class
5.Virtual function in derived class is calledVirtual function in derived class is called