1 class Car:
2 'Common base class for all cars'
3 carCount = 0
4
5 def __init__(self, name , model):
6 self.name = name
7 self.model = model
8 Car.carCount += 1
9
10 def printCarCount(self):
11 print "Total Car Counut in Town %d" % Car.carCount
12
13 def displayCarDeatils(self):
14 print "Name : ", self.name, ", Model: ", self.model
15
16 prius = Car('Prius','Toyota')
17 vitz = Car('Vitz','Toyota')
18 leaf = Car('Leaf','Nissan')
19 prius.displayCarDeatils()
20 prius.printCarCount()
The 'class' statement creates a new class.
- The class has a documentation string, which can be accessed via ClassName.__doc__
- The class_suite consists of all the component statements defining class members, data attributes and functions.
Creating Instance Objects
vitz = Car('Vitz','Toyota')
Accessing Attributes / methods of Instance
vitz .displayCarDeatils()
Destroying Objects (Garbage Collection)
del old_object
1 #!/usr/bin/python
2
3 class car: # define parent class
4 def display(self):
5 print 'I am car'
6
7 class SportCar(Car): # define child class
8 def display(self):
9 print 'I am sport car'
10
11 myCar = SportCar() # instance of child
12 myCar.myMethodx() # child calls overridden method
No comments:
Post a Comment