Skip to content

Instantly share code, notes, and snippets.

@tianweidut
Created November 18, 2015 12:38
Show Gist options
  • Save tianweidut/437ff8d9c662b35f1ef8 to your computer and use it in GitHub Desktop.
Save tianweidut/437ff8d9c662b35f1ef8 to your computer and use it in GitHub Desktop.
python 继承的小例子,使用子类的key
#coding: utf-8
class Base(object):
def __init__(self):
print 'Base __init__'
def update(self):
print 'Base update'
@classmethod
def add(cls):
print 'Base add'
class C1(Base):
KEY = 'C1-KEY'
def __init__(self):
print 'C1 init'
super(C1, self).__init__()
def update(self):
print 'C1 update'
self.clear()
super(C1, self).update()
@classmethod
def add(cls):
print 'C1 add'
cls.clear()
super(C1, cls).add()
@classmethod
def clear(cls):
print cls
print cls.KEY
print cls.__name__
class C2(C1):
KEY = 'C2-KEY'
def main():
c1 = C1()
print '**' * 10, 'c1 update'
c1.update()
print '**' * 10, 'c1 add'
C1.add()
c2 = C2()
print '**' * 10, 'c2 update'
c2.update()
print '**' * 10, 'c2 add'
C2.add()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment