Object Oriented Preparation
Overview
Object-oriented programming is a powerful way of organizing programs that you’ve already used in bits and pieces. While it’s great to use Python’s built in classes such as lists and strings, object-oriented programming really shines when you make your own classes. Today we’ll focus on the paradigm of thinking with objects and starting to make your own classes.
Basic Learning Objectives
Before class, you should be able to:
- Define classes, instances, and instance variables
- Identify the syntax to define your own class
- Create a program with multiple instances of a given class
Advanced Learning Objectives
After class, you should be able to:
- Define your own class
- Define a constructor
- Explain how instance variables and methods are used by objects
Resources
You should read the following:
- Runestone 13.1 Classes and Objects - the Basics
- Runestone 13.2 Objects Revisited
- Runestone 13.3 User Defined Classes
- Runestone 13.4 Adding Parameters to the Constructor
- Runestone 13.5 Adding Other Methods to a Class
Checks
Submit answers to the following to Moodle:
-
(Checkpoint 13.13.1) Define a class called
Bikethat accepts a string and a float as input, and assigns those inputs respectively to two instance variables,colorandprice. Assign to the variabletestOnean instance of Bike whose color is “blue” and whose price is 89.99. Assign to the variabletestTwoan instance ofBikewhose color is “purple” and whose price is 25.0. -
(Checkpoint 13.13.2) Create a class called
AppleBasketwhose constructor accepts two inputs: a string representing a color, and a number representing a quantity of apples. The constructor should initialize two instance variables:apple_colorandapple_quantity. Write a class method calledincreasethat increases the quantity by 1 each time it is invoked.