134 lines
3.7 KiB
Markdown
134 lines
3.7 KiB
Markdown
|
# Chapter 9. Classes
|
|||
|
|
|||
|
Objectives:
|
|||
|
|
|||
|
* Making an object from a class is called **instantiation**, and you work with **instances of a class**.
|
|||
|
* In this chapter you’ll write classes and create instances of those classes.
|
|||
|
* You’ll specify the kind of information that can be stored in instances, and you’ll define actions that can be taken with these instances.
|
|||
|
* You’ll also write classes that extend the functionality of existing classes, so similar classes can share code efficiently.
|
|||
|
* You’ll store your classes in modules and import classes written by other programmers into your own program files.
|
|||
|
|
|||
|
## Creating and using a class
|
|||
|
|
|||
|
```python
|
|||
|
class Dog:
|
|||
|
"""A Simple attempt to model a dog"""
|
|||
|
|
|||
|
def __init__(self, name, age):
|
|||
|
"""Initialize name and age attributes."""
|
|||
|
self.name = name
|
|||
|
self.age = age
|
|||
|
|
|||
|
def sit(self):
|
|||
|
"""Simulate a dog sitting in response to a command."""
|
|||
|
print(f"{self.name} is now sitting.")
|
|||
|
|
|||
|
def roll_over(self):
|
|||
|
"""Simulate rolling over in response to a command"""
|
|||
|
print(f"{self.name} rolled over!")
|
|||
|
```
|
|||
|
|
|||
|
### __init__() method
|
|||
|
|
|||
|
* A special method python run automatically whenever we create a new instance.
|
|||
|
* `self` in parameters is required in method definition, always come first
|
|||
|
* So method call will automatically pass the `self` argument, it's a reference to itself.
|
|||
|
|
|||
|
### Making an Instance from a Class
|
|||
|
|
|||
|
```python
|
|||
|
my_dog = Dog('Willie', 6)
|
|||
|
|
|||
|
(f"My dog's name is {my_dog.name}.")
|
|||
|
print(f"My dog is {my_dog.age} years old.")
|
|||
|
```
|
|||
|
|
|||
|
### Accessing Attributes
|
|||
|
|
|||
|
```python
|
|||
|
my_dog.name
|
|||
|
```
|
|||
|
|
|||
|
### Calling Methods
|
|||
|
|
|||
|
```python
|
|||
|
class Dog:
|
|||
|
--snip--
|
|||
|
|
|||
|
my_dog = Dog('Willie', 6)
|
|||
|
my_dog.sit()
|
|||
|
my_dog.roll_over()
|
|||
|
```
|
|||
|
|
|||
|
### Creating Multiple Instances
|
|||
|
|
|||
|
```python
|
|||
|
class Dog:
|
|||
|
--snip--
|
|||
|
|
|||
|
my_dog = Dog('Willie', 6)
|
|||
|
your_dog = Dog('Lucy', 3)
|
|||
|
|
|||
|
print(f"My dog's name is {my_dog.name}.")
|
|||
|
print(f"My dog is {my_dog.age} years old.")
|
|||
|
my_dog.sit()
|
|||
|
|
|||
|
print(f"\nYour dog's name is {your_dog.name}.")
|
|||
|
print(f"Your dog is {your_dog.age} years old.")
|
|||
|
your_dog.sit();
|
|||
|
```
|
|||
|
|
|||
|
## Working with Classes and Instances
|
|||
|
|
|||
|
After we create class, we can edit object
|
|||
|
|
|||
|
* need write **getter/setter functions** to access attributes
|
|||
|
* may need default value for attribute
|
|||
|
|
|||
|
```python
|
|||
|
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 get_descriptive_name(self):
|
|||
|
"""Return a neatly formatted descriptive name."""
|
|||
|
long_name = f"{self.year} {self.make} {self.model}"
|
|||
|
return long_name.title()
|
|||
|
|
|||
|
def read_odometer(self):
|
|||
|
"""Print a statement showing the car's mileage."""
|
|||
|
print(f"This car has {self.odometer_reading} miles on it.")
|
|||
|
|
|||
|
def update_odometer(self, mileage):
|
|||
|
"""Set the odometer reading to the given value."""
|
|||
|
self.odometer_reading = mileage
|
|||
|
|
|||
|
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()
|
|||
|
```
|
|||
|
|
|||
|
* `get_descriptive_name()` is a getter function, so we can get private attributes
|
|||
|
* `odometer_reading` has default value as 0
|
|||
|
|
|||
|
## Inheritance
|
|||
|
|
|||
|
Use **inheritance** when the class is a specialized version of another class.
|
|||
|
|
|||
|
* When one class *inherits* from another, it takes on the attributes and methods of the first class.
|
|||
|
* Original class is called **parent class**, New class is called **child class**
|
|||
|
* Child class can define new attributes/methods
|
|||
|
|
|||
|
### __init__() for a Child Class
|
|||
|
|
|||
|
Creating a `__init__()` of a child class:
|
|||
|
|
|||
|
1. Call `__init__()` from parent class, to initialize parent's attributes
|
|||
|
2.
|