fork download
  1. #parent class that is used to set food and family variables for child classes
  2. class Animal:
  3. def __init__(self,family,food):
  4. self.family = family
  5. self.food = food
  6.  
  7. def getFamily(self):
  8. return self.family
  9. def setFamily(self,newFamily):
  10. self.family = newFamily
  11. def getFood(self):
  12. return self.food
  13. def setFood(self,newFood):
  14. self.food = newFood
  15.  
  16. #child class that inherits from Animal class
  17. class Cow():
  18. def __init__(self,owner, family,food):
  19. self.owner = owner
  20. #use the Animal init function to set the family and food properties
  21. Animal.__init__(self,family,food)
  22. def setOwner(self,newOwner):
  23. self.owner = newOwner
  24. def getOwner(self):
  25. return self.owner
  26.  
  27. class Lion(Animal):
  28. def __init__(self,family,food):
  29. Animal.__init__(self,family,food)
  30.  
  31. mooingCow = Cow("Bob", "mammal","grass")
  32. print(Cow.__name__+"'s owner: " + mooingCow.getOwner() + ", family: " + mooingCow.getFamily())
  33. hungryLion = Lion("mammal","humans")
Runtime error #stdin #stdout #stderr 0.15s 26004KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 32, in <module>
AttributeError: 'Cow' object has no attribute 'getFamily'