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
classElectricCar(Car): """Represent aspects of a car, specific to electric vehicles."""
def__init__(self, make, model, year): """Initialize attributes of the parent class.""" super().__init__(make, model, year)
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
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_size = 70
defdescribe_battery(self): """Print a statement describing the battery size.""" print("This car has a " + str(self.battery_size) + "-kWh battery.")
假設 Car 類別中有個名為 fill_gas_tank() 方法。但此方法對電動車沒有作用,因此想要覆寫這個方法,示範如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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_size = 70
defdescribe_battery(self): """Print a statement describing the battery size.""" print("This car has a " + str(self.battery_size) + "-kWh battery.")
deffill_gas_tank(self): """Electric cars don't have gas tanks.""" print("This car doesn't need a gas tank!")
現在如果呼叫電動車的 fill_gas_tank() 方法, Python 會忽略 Car 類別中的 fill_gas_tank() 方法,而執行這裡的程式。 使用繼承這項功能,既可以讓子類別保留父類別中想要的東西,也能覆寫刪除不符合需要的內容。
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.")
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()
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()