"""A class that can be used to represent a car."""
classCar(): """A simple attempt to represent a car."""
def__init__(self, make, model, year): """Initialize attributes to describe a car.""" self.make = make self.model = model self.year = year self.odometer_reading = 0
defread_odometer(self): """Print a statement showing the car's mileage.""" print("This car has " + str(self.odometer_reading) + " miles on it.")
defupdate_odometer(self, mileage): """ Set the odometer reading to the given value. Reject the change if it attempts to roll the odometer back. """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!")
defincrement_odometer(self, miles): """Add the given amount to the odometer reading.""" self.odometer_reading += miles
"""A set of classes used to represent gas and electric cars."""
classCar(): """A simple attempt to represent a car."""
def__init__(self, make, model, year): """Initialize attributes to describe a car.""" self.make = make self.model = model self.year = year self.odometer_reading = 0
defread_odometer(self): """Print a statement showing the car's mileage.""" print("This car has " + str(self.odometer_reading) + " miles on it.")
defupdate_odometer(self, mileage): """ Set the odometer reading to the given value. Reject the change if it attempts to roll the odometer back. """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!")
defincrement_odometer(self, miles): """Add the given amount to the odometer reading.""" self.odometer_reading += miles
classBattery(): """A simple attempt to model a battery for an electric car."""
def__init__(self, battery_size=70): """Initialize the battery's attributes.""" self.battery_size = battery_size
defdescribe_battery(self): """Print a statement describing the battery size.""" print("This car has a " + str(self.battery_size) + "-kWh battery.")
defget_range(self): """Print a statement about the range this battery provdies.""" 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)
classElectricCar(Car): """Represent aspects of a car, specific to electric vehicles."""
def__init__(self, make, model, year): """ Initialize attributes of the parent class. Then initialize attributes specific to an electric car. """ super().__init__(make, model, year) self.battery = Battery()
"""A set of classes that can be used to represent electric cars."""
from car import Car
classBattery(): """A simple attempt to model a battery for an electric car."""
def__init__(self, battery_size=70): """Initialize the battery's attributes.""" self.battery_size = battery_size
defdescribe_battery(self): """Print a statement describing the battery size.""" print("This car has a " + str(self.battery_size) + "-kWh battery.")
defget_range(self): """Print a statement about the range this battery provdies.""" 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)
classElectricCar(Car): """Represent aspects of a car, specific to electric vehicles."""
def__init__(self, make, model, year): """ Initialize attributes of the parent class. Then initialize attributes specific to an electric car. """ super().__init__(make, model, year) self.battery = Battery()
ElectricCar 類別需要用到 Car 這個父類別,因此直接把 Car 類別匯入這個模組內。
現在就可以從每個模組中匯入需要的類別,並根據需要建立各種類型的車子實例:
1 2 3 4 5 6 7 8
from car import Car from electric_car import ElectricCar