A game with 100 enemies
You make a game. It has 100 zombies. Each zombie has a name, some health and a way to attack.
Do you write the same code 100 times? No. You write a plan once. Then you make 100 copies from the plan.
This is the big idea of object-oriented design. Object-oriented means a program is built from objects.
Key words
| Word | What it means | Example |
|---|---|---|
| Class | A plan for making objects. It says what they know and what they can do. | Character |
| Object | One thing made from a class. Also called an instance. | Sam, a Character |
| Method | A small named job that belongs to a class. | take_damage() |
| Attribute | A value that belongs to an object. | health |
| Encapsulation | Keep data and the code that uses it together, and protect the data. | |
| Data abstraction | Show only what the user needs. Hide the rest. | |
| Inheritance | A new class takes everything from an old class, then adds or changes things. | |
| Polymorphism | Different objects answer the same method call in their own way. | |
| Design pattern | A tested answer to a problem that comes up again and again. |
The cells students fill in are left blank in this preview.
A class is a plan
A class is like the plan for a house. You can build many houses from one plan.
Each house is an object. Each object has its own attributes, such as a name and health. Each object can do things. These things are methods.
Making an object from a class is called creating an instance.
Try it: a class and two objects
class Character:
def __init__(self, name, health):
self.name = name
self.health = health
def speak(self):
return self.name + " says: Hello"
sam = Character("Sam", 10)
ben = Character("Ben", 7)
print(sam.speak())
print(ben.health)
# Make a third Character and print what it says.
Quick check: class or object?
Have a go
Quick check: class or object?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
The four characteristics
| Idea | In simple words | Real life |
|---|---|---|
| Encapsulation | Data and its methods live together. Outside code cannot break the data. | A bank machine: you press buttons, you cannot reach the cash |
| Data abstraction | Show the simple view. Hide the details. | A car pedal: you press it, you do not see the engine |
| Inheritance | A child class gets everything from its parent class. | A Zombie is a Character with a different voice |
| Polymorphism | Same call, different result for each type. | Say "make a sound" to a cat and a dog |
The cells students fill in are left blank in this preview.
Try it: protect the health
class Character:
def __init__(self, health):
self._health = health # the _ means: keep out
def take_damage(self, amount):
self._health = max(0, self._health - amount)
def health(self):
return self._health
c = Character(10)
c.take_damage(50)
print(c.health()) # 0, never below 0
Try it: inheritance and polymorphism
class Animal:
def speak(self):
return "..."
class Cat(Animal):
def speak(self):
return "Meow"
class Dog(Animal):
def speak(self):
return "Woof"
for pet in [Animal(), Cat(), Dog()]:
print(pet.speak())