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())
# _*_ coding :UTF-8 _*_ # 开发人员:C # 开发时间:2019/4/30 20:13 # 文件名称:my_car.py # 开发软件:PyCharm from car2 import Car my_new_car = Car('audi', 'a4', 2016) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer()
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())
from car2 import Car, ElectricCar # from car2 import * # import car2 my_car = Car("audi", "A8", 2018) # my_car = car2.Car("audi", "A8", 2018) print(my_car.get_descriptive_name()) my_car.odometer_reading = 30 my_car.read_odometer() my_new_car = ElectricCar("benz", "a700", 2019) # my_new_car = car2.ElectricCar("benz", "a700", 2019) print(my_new_car.get_descriptive_name()) my_new_car.battery.describe_battery() my_new_car.battery.get_range()
### 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
GREY = (210, 210, 210) WHITE = (255, 255, 255) RED = (255, 0, 0) PURPLE = (255, 0, 255) SCREENWIDTH = 400 SCREENHEIGHT = 500 size = (SCREENWIDTH, SCREENHEIGHT) screen = pygame.display.set_mode(size) pygame.display.set_caption("Car Racing") # This will be a list that will contain all the sprites we intend to use in our game. all_sprites_list = pygame.sprite.Group() playerCar = Car(RED, 20, 30) playerCar.rect.x = 200 playerCar.rect.y = 300 # Add the car to the list of objects all_sprites_list.add(playerCar) # Allowing the user to close the window... carryOn = True clock = pygame.time.Clock() while carryOn: for event in pygame.event.get(): if event.type == pygame.QUIT: carryOn = False