OOP Lecture 06
OOP? Stands for object oriented programming. You’ve been using it all along. Anything you use the dot ‘.’ on is an object.
Methods and Variables Objects have methods and variables # Getting a variable self.rect.x # Using a method mylist.append(54) # Getting a variable self.rect.x # Using a method mylist.append(54)
Classes All objects belong to a class Objects are instances of a class Classes are like a blueprint for objects
Making Classes class Ball(): def __init__(self, name): self.bounce = False self.name = name class Ball(): def __init__(self, name): self.bounce = False self.name = name
Abstraction Getting all the necessary information for you needs.
Adding Methods class Ball(): def __init__(self, name): self.bounce = 5 self.name = name def bounce_ball(self): self.bounce = True class Ball(): def __init__(self, name): self.bounce = 5 self.name = name def bounce_ball(self): self.bounce = True
Using the Classes This means instantiating them, and creating different versions of them ball1 = Ball("Tennis") ball2 = Ball("Basket") ball3 = Ball("Base") ball1.bounce_ball() print ball1.name print ball1.bounce print ball2.bounce ball1 = Ball("Tennis") ball2 = Ball("Basket") ball3 = Ball("Base") ball1.bounce_ball() print ball1.name print ball1.bounce print ball2.bounce
Calling Methods Methods are functions on objects. We call them using the ‘.’ operator
Student Class
Inheritance