def main(): mammal = animals.Mammal('Regular Animal') dog = animals.Dog() cat = animals.Cat() print('Animals and the sound they make') print('-----------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat) print() show_mammal_info('I am a string')
def main(): # Create a Mammal object, a Dog object, and a Cat object. mammal = animals.Mammal('regular animal') dog = animals.Dog() cat = animals.Cat() # display information about each one. print('Here are some animals and') print('the sounds they make.') print('--------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat)
def main(): # Create a Mammal object, a Dog object, and # a Cat object. mammals = list() mammals.append(animals.Mammal('regular animal')) mammals.append(animals.Dog()) mammals.append(animals.Cat()) mammals.append(animals.Siberian_Husky()) # Display information about each one. print('Here are some animals and') print('the sounds they make.') print('--------------------------') for mammal in mammals: show_mammal_info(mammal) print()
def main(): mammal = animals.Mammal('regular animal') dog = animals.Dog() cat = animals.Cat() # Display information about each one. print('Here are some animals and' + 'the sounds they make.') print('--------------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat)
def main(): mammal = animals.Mammal('regular animal') dog = animals.Dog() cat = animals.Cat() print('Here are some animals and\nthe sound they make:') print('-------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat)
def main(): # Utworzenie obiektów klas # Mammal, Dog i Cat. mammal = animals.Mammal('zwierzę') dog = animals.Dog() cat = animals.Cat() # Wyświetlenie informacji o każdym zwierzęciu. print('Oto kilka przykładów zwierząt') print('i wydawanych przez nie dźwięków.') print('--------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat)
def main(): # Создать объект Mammal, объект Dog # и объект Cat. mammal = animals.Mammal('Обычное животное') dog = animals.Dog() cat = animals.Cat() # Показать информацию о каждом животном. print('Вот несколько животных и') print('звуки, которые они издают.') print('--------------------------') show_mammal_info(mammal) print() show_mammal_info(dog) print() show_mammal_info(cat)
def __init__(self, species): self.__species = species def show_species(self): print('I am a', self.__species) def make_sound(self): print('Grrrrr') # dog class inherits species, show_species and make_sound class Dog(Mammal): def __init__(self): Mammal.__init__(self, 'Dog') # overrides species with "Dog" def make_sound(self): # overrides make_sound print('Woof! Woof!') # from program file polymorphism_demo.py import animals mammal = animals.Mammal('regular animal') # create mammal object dog = animals.Dog() # create dog object # call show_species and make_sound methods from superclass mammal.show_species() mammal.make_sound() # call show_species and make_sound methods from subclass dog.show_species() dog.make_sound() ### OUTPUT ### # I am a regular animal # Grrrrr # I am a Dog # Woof! Woof!
import animals mammal = animals.Mammal('Regular mammal') mammal.show_species() mammal.make_sound() cat = animals.Cat() cat.show_species() cat.make_sound() dog = animals.Dog() dog.show_species() dog.make_sound()