-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAnimalTest.py
41 lines (31 loc) · 1.02 KB
/
AnimalTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from abc import ABC, abstractmethod
from typing import List
class AnimalTest:
@staticmethod
def main(*args):
at: AnimalTest = AnimalTest()
at.makeSomeAnimals()
def makeSomeAnimals(self):
dog: Animal = self.Dog()
cat: Animal = self.Cat()
# treat dogs and cats as their supertype, Animal
animals: List[Animal] = []
animals.append(dog)
animals.append(cat)
for animal in animals: animal.makeSound() # can call makeSound on any Animal
class Animal(ABC):
@abstractmethod
def makeSound(self) -> None:
raise NotImplementedError
class Dog(Animal):
def makeSound(self) -> None:
self.bark()
def bark(self) -> None:
print('Woof')
class Cat(Animal):
def makeSound(self) -> None:
self.meow()
def meow(self) -> None:
print('Meow')
if __name__ == "__main__":
AnimalTest.main()