Docsity

Building blocks of object oriented programming CS 1301, Summaries of Object Oriented Programming

Toggle sidebar
bg1
BUILDING BLOCKS OF OBJECT ORIENTED
PROGRAMMING
1. CLASS A class is the blueprint. It defines what something is and what it can do. It doesn't create
anything by itself — it's just the template.
class Car:
2. CONSTRUCTOR The special method __init__ that runs automatically when you create an
instance. It sets up the object.
def __init__(self, make, model, year):
3. PARAMETERS The inputs the constructor (or any method) accepts. self is always first — it refers
to the object being created. The rest are values you pass in.
def __init__(self, make, model, year):
# ↑ ↑ ↑ ↑
# self param param param
4. INSTANCE VARIABLES (attributes) Variables stored ON the object using self. — they persist as
long as the object exists.
self.make = make
self.model = model
self.year = year
5. METHODS Functions that belong to the class. Always take self as the first parameter.
def get_make(self): # getter
return self.make
def set_make(self, make): # setter
self.make = make
def describe(self): # regular method
return str(self.year) + " " + self.make + " " + self.model
6. INSTANCE An actual object created FROM the class blueprint. This is where things come to life.
pf3

Partial preview of the text

Download Building blocks of object oriented programming CS 1301 and more Summaries Object Oriented Programming in PDF only on Docsity!

BUILDING BLOCKS OF OBJECT ORIENTED

PROGRAMMING

1. CLASS A class is the blueprint. It defines what something is and what it can do. It doesn't create anything by itself — it's just the template.

class Car:

2. CONSTRUCTOR The special method init that runs automatically when you create an instance. It sets up the object.

def init(self, make, model, year):

3. PARAMETERS The inputs the constructor (or any method) accepts. self is always first — it refers to the object being created. The rest are values you pass in.

def init(self, make, model, year):

↑ ↑ ↑ ↑

self param param param

4. INSTANCE VARIABLES (attributes) Variables stored ON the object using self. — they persist as long as the object exists.

self.make = make self.model = model self.year = year

5. METHODS Functions that belong to the class. Always take self as the first parameter.

def get_make(self): # getter return self.make

def set_make(self, make): # setter self.make = make

def describe(self): # regular method return str(self.year) + " " + self.make + " " + self.model

6. INSTANCE An actual object created FROM the class blueprint. This is where things come to life.

my_car = Car("Toyota", "Camry", 2020 )

↑ ↑ ↑ ↑

class make model year

7. ACCESSING ATTRIBUTES AND METHODS Using dot notation on the instance:

python

print(my_car.make) # accesses attribute directly

print(my_car.get_make()) # accesses attribute via getter

print(my_car.describe()) # calls a method

my_car.set_make("Honda") # calls a setter to change a value

Putting it all together:

python

class Car: # 1. CLASS def init(self, make, model, year): # 2. CONSTRUCTOR

3. PARAMETERS: make, model,

year self.make = make # 4. INSTANCE VARIABLES self.model = model self.year = year

def get_make(self): # 5. METHOD (getter) return self.make

def set_make(self, make): # 5. METHOD (setter) self.make = make

def describe(self): # 5. METHOD (regular) return str(self.year) + " " + self.make + " " + self.model

my_car = Car("Toyota", "Camry", 2020 ) # 6. INSTANCE

print(my_car.describe()) # 7. ACCESSING METHODS