11. What is polymorphism

۰ نظر گزارش تخلف
Farnaz Baghban
Farnaz Baghban

#8 Python Programming Exercise

Construct a hierarchy of classes with inheritance. The aim is to construct a Vehicle abstract class. This class get 2 parameters from outside via the constructor - brand and year. Use keyword arguments.

Create 2 child classes: Bus and Car . There is a common function in these classes that can calculate the fuel consumption - just print out some random value for demonstration purposes.

Good luck!
=========================
#8 Python Programming Solution

class Vehicle:

def __init__(self, brand, year):
self.brand = brand
self.year = year

def fuel_consumption(self):
pass


class Bus(Vehicle):

def __init__(self, brand, year):
super().__init__(brand, year)

def fuel_consumption(self):
print('Fuel consumption for bus...')


class Car(Vehicle):

def __init__(self, brand, year):
super().__init__(brand, year)

def fuel_consumption(self):
print('Fuel consumption for car...')


bus = Bus(brand='Ford', year=1957)
bus.fuel_consumption()

car = Car(brand='BMW', year=1998)
car.fuel_consumption()

نظرات

در حال حاضر امکان درج نظر برای این ویدیو غیرفعال است.

توضیحات

11. What is polymorphism

۰ لایک
۰ نظر

#8 Python Programming Exercise

Construct a hierarchy of classes with inheritance. The aim is to construct a Vehicle abstract class. This class get 2 parameters from outside via the constructor - brand and year. Use keyword arguments.

Create 2 child classes: Bus and Car . There is a common function in these classes that can calculate the fuel consumption - just print out some random value for demonstration purposes.

Good luck!
=========================
#8 Python Programming Solution

class Vehicle:

def __init__(self, brand, year):
self.brand = brand
self.year = year

def fuel_consumption(self):
pass


class Bus(Vehicle):

def __init__(self, brand, year):
super().__init__(brand, year)

def fuel_consumption(self):
print('Fuel consumption for bus...')


class Car(Vehicle):

def __init__(self, brand, year):
super().__init__(brand, year)

def fuel_consumption(self):
print('Fuel consumption for car...')


bus = Bus(brand='Ford', year=1957)
bus.fuel_consumption()

car = Car(brand='BMW', year=1998)
car.fuel_consumption()