python類的繼承

#! /usr/bin/python3
# -*-coding:UTF-8-*-

class Anamil(object):
    def run(self):
        print('Animal is running')

    def __eat(self):
        print('私有方法eat,繼承的子類不能調用')

class Dog(Anamil):      #繼承父類Animal
    pass

class Cat(Anamil):
    def run(self):      #多態
        print('覆蓋父類的run方法')

dog_object = Dog()
dog_object.run()    #調用父類的run方法
# dog_object.__eat()    #報錯
cat_object = Cat()
cat_object.run()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章