Advertisement
nanenj

Python OOP example

Dec 2nd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. class Animal:
  2.     def __init__(self, name):
  3.         self.name = name
  4.  
  5.     def speak(self):
  6.         print("This animal is named: " + self.name)
  7.         print("It doesn't make a specific sound.")
  8.  
  9. a1 = Animal("fred")
  10. a1.speak()
  11.  
  12. class Dog(Animal):
  13.     def __init__(self, name):
  14.         super().__init__(name)
  15.  
  16.     def speak(self):
  17.         print("This animal is named: " + self.name)
  18.         print("It goes Arf!")
  19.  
  20. d1 = Dog("Fido")
  21. d1.speak()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement