Lab9 OOP Slides
Lab9 OOP Slides
(OOP)
OBJECT ?
Brand Color
Price
Steer Horn
Shift Gear
CAR OBJECT
Car
• Brand
• Power
• Color
• Country of Origin
• Price
• Steer
• Shift Gear
• Horn
WHAT IS AN OBJECT ?
• Real-world objects share two characteristics:
• They all have state and behavior.
• Ex : Cars have state (color, price) and behaviour (steering, accelerating)
Dogs have state (name, breed) and behaviour (barking, wagging tail)
• An object stores its state in data attributes and exposes its behavior through
methods
WHAT IS A CLASS ?
• In the real world, there are many individual objects all of the same kind.
• Each automobile was built from the same set of blueprints and therefore contains the same
components.
• In object-oriented terms, we say that your automobile is an instance of the class of
objects known as automobiles.
class definiton
ATTRIBUTES
• Data and procedure belonging to that class.
• data attributes:
• Data that makes up the class.
• A car has a brand name, color, price etc.
• methods:
• Functions that only work with the class
• A car can sterr, accelerate etc.
How we access an attribute
OUTPUT
USING __init__
Special __init__ method initialize some data attributes
data attribute
USING __init__
Special method to create an instance
parameter to refer an instance of the class
data attribute
Initialization
OUTPUT
METHODS
PYTHON OBJECTS
• EVERYTHING IN PYTHON IS AN OBJECT !
OUTPUT
Defining Your Own Print Method __str__
OUTPUT
Special operators
• +, -, ==, <, >, len(), print, and many others
https://docs.python.org/3/reference/datamodel.html#basic-customization
• like print, can override these to work with your class
• define them with double underscores before/after
WHY DO WE USE OBJECTS ?
Name
Surname
E-mail address
Student ID
Grade
Student1 Student2 Student3