Python
Define a class called Animal that abstracts animals and supports three methods:
The class must support supports a two, one, or no input argument constructor.
Then define Bird as a subclass of Animal and change the behavior of method speak() in class Bird.
>>> snoopy = Animal('dog', 'bark')
>>> snoopy.speak()
I am a dog and I bark.
>>> tweety = Animal('canary')
>>> tweety.speak()
I am a canary and I make sounds.
>>> animal = Animal()
>>> animal.speak()
I am a animal and I make sounds.
>>> daffy = Bird()
>>> daffy.speak()
quack! quack! quack!
""" Python program to for class Animal """ class Animal: def __init__(self, sp = 'animal', lan = 'sounds'): self.species = sp self.language = lan def setSpecies(self, species): self.species = species def setLanguage(self, language): self.language = language def speak(self): if self.language != "sounds": print("I am a " + self.species + " and I " + self.language) else: print("I am a " + self.species + " and I make " + self.language) class Bird(Animal): def speak(self): print("quak! "*3) if __name__ == '__main__': snoopy = Animal('dog', 'bark') snoopy.speak() tweety = Animal('canary') tweety.speak() animal = Animal() animal.speak() daffy = Bird() daffy.speak()
Get Answers For Free
Most questions answered within 1 hours.