Added gitignore

master
Jason Zhu 2020-09-05 00:21:38 +10:00
parent 4e37c9fa9a
commit c1cec32f9a
3 changed files with 104 additions and 3 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
.vscode

View File

@ -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.
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

View File

@ -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()
my_new_car.read_odometer()
my_tesla = ElectricCar('telsa', 'model s', 2019)
print(my_tesla.get_descriptive_name())