Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. # Parent class
  2. class Dog:
  3.  
  4.     species = 'mammal'
  5.  
  6.     def __init__(self, name, age):
  7.         self.name = name
  8.         self.age = age
  9.  
  10. # Child class (inherits from Dog class)
  11. class RussellTerrier(Dog):
  12.     def run(self, speed):
  13.         return "{} runs {}".format(self.name, speed)
  14.  
  15.  
  16. # Child class (inherits from Dog class)
  17. class Bulldog(Dog):
  18.     def run(self, speed):
  19.         return "{} runs {}".format(self.name, speed)
  20.  
  21.  
  22. # Child classes inherit attributes and
  23. # behaviors from the parent class
  24. jim = Bulldog("Jim", 12)
  25. print(jim.name)
  26.  
  27. # Child classes have specific attributes
  28. # and behaviors as well
  29. print(jim.run("slowly"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement