继承
class Character(object):
def __init__(self, name):
self.health = 100
self.name = name
def printName(self):
print (self.name)
class Blacksmith(Character):
def __init__(self, name, forgeName):
super(Blacksmith, self).__init__(name)
self.forge = Forge(forgeName)
class Forge:
def __init__(self, forgeName):
self.name = forgeName
bs = Blacksmith("Andy", "Dufresne")
bs.printName()
print (bs.forge.name)
覆盖
class BaseClass(object):
def test(self):
print ("ham")
class InClass(BaseClass):
def test(self):
print ("hammer time")
i = InClass()
i.test()
可以打印出子类:
print (BaseClass.__subclasses__())
[<class '__main__.InClass'>]
>>>