Advertisement
dewabe

SimpleSample: Classes 2

Sep 4th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. '''
  2. SimpleSample: Class 2
  3. '''
  4.  
  5. class Vehicle(object):
  6.     # Init value of main class (only color)
  7.     def __init__(self, color):
  8.         self._color = color
  9.  
  10.     # Print color
  11.     def color(self):
  12.         print("Vehicle color is {color}".format(color=self._color))
  13.  
  14.     # Print details
  15.     # By default there won't be any tires :(
  16.     def details(self):
  17.         print("Vehicle color is {color} but I have no tires"\
  18.             .format(color=self._color))
  19.  
  20. class Car(Vehicle):
  21.     # Init values of Car class (tires and color)
  22.     # Note super!
  23.     def __init__(self, tires, color):
  24.         super(Car, self).__init__(color)
  25.         self._tires = tires
  26.  
  27.     # Print the details
  28.     # Override the "main" classes details function and print
  29.     # with number of tires :-)
  30.     def details(self):
  31.         print("My car is nice. It is coloured as {color} and it have {tires} tires"\
  32.             .format(color=self._color, tires=self._tires))
  33.  
  34.  
  35. myCar = Car(4, "red")
  36. myCar.color()
  37. myCar.details()
  38.  
  39. notCar = Vehicle("blue")
  40. notCar.color()
  41. notCar.details()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement