更新
This commit is contained in:
33
Python-100-Days/Day01-15/Day09/pet.py
Normal file
33
Python-100-Days/Day01-15/Day09/pet.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Pet(object, metaclass=ABCMeta):
|
||||
|
||||
def __init__(self, nickname):
|
||||
self._nickname = nickname
|
||||
|
||||
@abstractmethod
|
||||
def make_voice(self):
|
||||
pass
|
||||
|
||||
|
||||
class Dog(Pet):
|
||||
|
||||
def make_voice(self):
|
||||
print('%s: 汪汪汪...' % self._nickname)
|
||||
|
||||
|
||||
class Cat(Pet):
|
||||
|
||||
def make_voice(self):
|
||||
print('%s: 喵...喵...' % self._nickname)
|
||||
|
||||
|
||||
def main():
|
||||
pets = [Dog('旺财'), Cat('凯蒂'), Dog('大黄')]
|
||||
for pet in pets:
|
||||
pet.make_voice()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user