# car1.odometer_reading = 800 # not good practice car1.set_odometer(800) # we can put logic in the method print(car1.read_odometer()) print(car1.odometer_reading) car1.set_odometer(1500) print(car1.read_odometer()) car1.increment_odometer(-500) assert car1.read_odometer() == 1500 print(car1.read_odometer()) car1.increment_odometer(400) print(car1.read_odometer()) ecar1 = ElectricCar('Tesla', 'Model Y', '2020') print(ecar1.get_description()) ecar1.set_color('Blue') print(ecar1.get_description()) print('-----------------------') ecar1.test_method() # ecar1.battery_size # Object Oriented Programming # - Class, Object # - constructor (__init__() ) # - Inheritance (single class, multiple class) # - relationship between Parent and Child : Parent >> Child # car1.battery_size, car1.test_method() parent does not have access to child attributes/methods # - self keyword, super() method , difference # - Overriding(due Inheritance) - rewriting parent attributes/method in child class
# mycar = Car() # Car is the class, mycar is an object, in this line we are creating instance of the (instantiation) mycar = Car("BMW", "530xi", "black") yourcar = Car("Lexus", "lexus RX", "silver") print("------------------") mycar.get_description() mycar.drive() mycar.set_odometer_reader(50) mycar.odo_reader = 45 # this is the direct access to the instance variables mycar.set_odometer_reader(65) mycar.set_odometer_reader(34) mycar.set_odometer_reader(545) mycar.set_odometer_reader(9504) mycar.colorofthecar = 'RED' mycar.get_description() # yourcar.do_something() print("------------------") yourcar.get_description() yourcar.drive() yourcar.set_odometer_reader(30) yourcar.get_description() print("--- Increment ---") print("--- Electric Car instances ---") my_ev = ElectricCar("tesla", "model x", "blue") my_ev.drive() my_ev.get_description()
# Execution # we create objects here. Objects: mycar, yourcar but only class: Car from classes.cars import Car, ElectricCar mycar = Car("BMW", "530xi", "black") yourcar = Car("Lexus", "Lexus IS", "silver") yourcar.drive() mycar.drive() mycar.get_description() # mycar.odo_reader = 20 mycar.set_odometer_reader(60) # we put on some miles on the car #mycar.__odo_reader = 30 # doesn't do anything # can only alter this variables value from inside the class mycar.get_description() # print(mycar.__odo_reader) --> throws an attribute error # because of encapsulation, meaning we cant access it from outside print() # 4/03/2021 EV sub-class (child class) print("--- This is electric car instances ---") my_ev = ElectricCar("tesla", "model x", "blue") my_ev.get_description() # we have access already # because we inherit attributes and behavior from parent class #print(f"The battery size on ev is: {my_ev.battery_size}") my_ev.get_battery_info() # here you can see same f() get description is now coming from an eletric car child class # We have overwritten (written on top of) the parent function my_ev.get_description()
mycar.set_odometer_reader(25) mycar.set_odometer_reader(60) mycar.__odo_reader = 20 mycar.get_description() mycar.color = 'RED' mycar.get_description() print("===================================") yourcar.get_description() yourcar.drive() yourcar.set_odometer_reader(30) yourcar.get_description() # yourcar.do_something() # 04/03/2021 print("-------Electric car instances----------") # my_ev = ElectricCar('tesla', 'model x', 'blue') my_ev = ElectricCar('tesla', 'model x', 'blue', 100) my_ev.drive() my_ev.get_description() print('battery size: ', my_ev.battery_size) # mycar.battery_size # only child has battery_size attribute, parent does not see that attribute # Car (state, behaviour) -> ElectricCar(state, behaviour) # we can create new methods(functions, available to child only my_ev.get_battery_info() # we can override the method that parent had (OOPs - Method Overriding) my_ev.get_description() mycar.drive() my_ev.drive()
print(car1.get_description()) print(car1.read_odmeter()) car1.odometer_reading = 1000 # not good practice print(car1.read_odmeter()) car1.set_odometer(800) print(car1.read_odmeter()) car1.set_odometer(1100) print(car1.read_odmeter()) car1.increment_odometer(-500) print(car1.read_odmeter()) car1.increment_odometer(400) print(car1.read_odmeter()) ecar1 = ElectricCar("tesla", "model y", 2020, 100) print(ecar1.get_description()) ecar1.set_color("Blue") print(ecar1.get_description()) print("____________________________________") ecar1.test_method() # OOP # - Class, Object # - Inheritance (single class, multiple class) # - Constructor (__init__) # - self keyword, super() method, difference # - Overrriding(due Inheritance) - rewriting parent attributes/method in child class # - relatitonship between Parent and child: Parent >> Child