2017年8月13日 星期日

[python] inheritance

Inheritance

class animalBaseClass:
    animallegs = 4
 
    def walk(self):
        print('走動')

    def cry(self):
        print('吼叫')

    def getLegsNum(self):
        print(self.animallegs)

class dogClass(animalBaseClass): 
    def __init__(self):
        print('我是小狗')

wanko = dogClass()
wanko.walk()
wanko.cry()
wanko.getLegsNum()

Override

class animalBaseClass:
  animallegs = 2
  def walk(self):
    print('走動')
  def cry(self):
    print('吼叫')

class birdClass(animalBaseClass):
  def __init__(self):
    print('我是小鳥')
  def cry(self):
    print('啾啾')

piyo_suke = birdClass()
piyo_suke.walk()
piyo_suke.cry()