from car2 import Car from electric_car import ElectricCar my_beetle = Car('volkswagen', 'beetle', 2019) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla', 'roadster', 2019) print(my_tesla.get_descriptive_name())
def get_range(self): """Print statement about range battery provides""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """Represents aspects of a car, specifgic to eletric vehicles""" def __init__(self, make, model, year): """Initialise attributes of parent class""" super().__init__(make, model, year) self.battery = Battery() # the class ElectricCar needs access to its parent class Car so Car is imported from car2 my_tesla = ElectricCar("tesla", "model s", 2016) # dot notation used a access ElectricCar print(my_tesla.get_descriptive_name()) my_new_car = Car("Audi", "A4", 2016) print(my_new_car.get_descriptive_name()) my_new_car = Car("Land Rover", "Defender", 1969) print(my_new_car.get_descriptive_name())
### Chapter 9: Classes ## Importing Classes # Importing a Single Class (Cont'd) from car2 import Car # Imports the Car class from car2.py in the same directory my_new_car = Car( 'chevrolet', 'silverado', '2020') # Creates new instance of Car from car2.py for my new car print( my_new_car.get_descriptive_name() ) # Calls the get_descriptive_name() method from in Car class imported from car2.py for the instance of my new car my_new_car.odometer_reading = 23 # Sets the odometer reading of my_new_car instance of Car class imported from car2.py my_new_car.read_odometer( ) # Calls the read_odometer() method from Car Class imported from car2.py for the instance of my new car