In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code.
class Car(object):
def __init__(self, model, passengers, color, speed):
self.model = model
self.
passengers = passengers
passengers = passengers
self.color = color
self.speed = speed
def accelerate(self):
self.speed = self.speed + 2
print (self.speed)
bmw = Car("BMW", 4, "red", 5)
ferrari = Car("Ferrari", 2, "black", 10)
ford = Car("Ford", 6, "blue", 6)
bmw.accelerate()
print (bmw.color)
ferrari.accelerate()
print (ferrari.color)
ferrari.accelerate() #note that the speed has been updated from the previous accelerate call
print (ford.passengers)
ford.accelerate()



0 Comments