简体   繁体   中英

Python Beginner: Setting a variable in a subclass

Working through a beginner course to Python and currently on classes. As an example, the book is using descriptions of Cars/Electric cars to explain classes and sub-classes, etc.

Here is the code:

class Car():
    ''' 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

    def car_description(self):
        ''' Return a neatly formatted descriptive name '''
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

class ElectricCar(Car):
    # When we put (Car) in the class definition, a child class is created with the attributes of Car.
    ''' Represents aspects of a car, specific to electric vehicles. ''' 

    def __init__(self, make, model, year):
        ''' Initalize attributes of the parent class. '''
        super().__init__(make,model,year)
        self.battery = Battery()

class Battery():
    ''' 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

    def describe_battery(self):
        ''' Print a statement describing the battery size. ''' 
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

    def get_range(self):
        ''' Print a statement about the range this 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)

my_tesla = ElectricCar('tesla','model s', 2016)
print(my_tesla.car_description())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

In the class Battery(), the method get_range shows two possible battery sizes (70 and 85), along with their respective ranges.

In the init for Battery, the battery size is by default set to 70 kWh.

How would I call on Battery() to set the battery size to 85 kWh for a vehicle?

Just give it the value:

self.battery = Battery(85)

The default values are used only when there are no values passed into the function, otherwise, the arguments that are passed are used.

And as @jasonharper suggested, you can add a parameter to the ElectricCar 's __init__() method that specifies the battery size:

def __init__(self, make, model, year, batterySize):
    ''' Initalize attributes of the parent class. '''
    super().__init__(make,model,year)
    self.battery = Battery(batterySize)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM