diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/chap9_classes.md b/chap9_classes.md index dc3db37..7c055e8 100644 --- a/chap9_classes.md +++ b/chap9_classes.md @@ -130,5 +130,78 @@ Use **inheritance** when the class is a specialized version of another class. Creating a `__init__()` of a child class: -1. Call `__init__()` from parent class, to initialize parent's attributes -2. \ No newline at end of file +1. Call `__init__()` from parent class use `super().__init__()`, to initialize parent's attributes (as shown below) +2. Add specialized attributes + +```python +class ElectricCar(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) +``` + +* `super()` is a special function that allows us to call a method from parentclass. It will initialize all inherited attributes for ElectricCar + +### Defining Attributes and Methods for the Child Class + +Add attributes/methods for child class + +```python +class ElectricCar(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 = 75 + + def describe_battery(self): + """Print a statement describing the battery size.""" + print(f"This car has a {self.battery_size}-kWh battery.") +``` + +* `self.battery_size` is attribute unique to ElectricCar, not for any parent class +* You can add attribute/method for child class as many as you want. + +### Overriding Methods from Parent Class + +Python can disregard parent class's method and only use redefined method in child class + +```python +class Car: + """A simple attempt to represent a car.""" + + def __init__(self, make, model, year): + """Initialize attributes to describe a car.""" + ... + self.gas_tank = 0 + + ... + + def fill_gas_tank(self, gas): + """Add gas into tank""" + self.gas_tank = self.gas_tank + gas + +class ElectricCar(Car): + """Represent aspects of a car, specific to electric vehicles.""" + + ... + + def fill_gas_tank(self): + """Electric cars don't have gas tanks""" + print("This car doesn't need gas tank!") +``` + +* `fill_gas_tank()` override parent method, with argument list changed + +### Instance as Attributes + +Sometimes we can use an instance of an class (object) as an attribute of another class. + +e.g. After grouping multiple all battery related attributes/method into a new class + diff --git a/src/car.py b/src/car.py index dad4990..e1f1a7d 100644 --- a/src/car.py +++ b/src/car.py @@ -7,6 +7,7 @@ class Car: self.model = model self.year = year self.odometer_reading = 0 + self.gas_tank = 0 def get_descriptive_name(self): """Return a neatly formatted descriptive name.""" @@ -21,7 +22,33 @@ class Car: """Set the odometer reading to the given value.""" self.odometer_reading = mileage + def fill_gas_tank(self, gas): + """Add gas into tank""" + self.gas_tank = self.gas_tank + gas + +class ElectricCar(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 = 75 + + def describe_battery(self): + """Print a statement describing the battery size.""" + print(f"This car has a {self.battery_size}-kWh battery.") + + def fill_gas_tank(self): + """Electric cars don't have gas tanks""" + print("This car doesn't need gas tank!") + my_new_car = Car('audi', 'a4', 2019) print(my_new_car.get_descriptive_name()) my_new_car.update_odometer(23) -my_new_car.read_odometer() \ No newline at end of file +my_new_car.read_odometer() + +my_tesla = ElectricCar('telsa', 'model s', 2019) +print(my_tesla.get_descriptive_name()) \ No newline at end of file