In Class: Week 11 Tuesday and Thursday:
Defining Classes
Create a week 11 subdirectory in your
cs21/inclass directory by running update21:
$ update21
$ cd
$ cd cs21/class/w11-classes
Topics
- Object-Oriented Programming
- Defining Classes
- Class methods
- The constructor, __init__
- String conversion, __str__
- The implicit self parameter
- Access methods a.k.a. getters
- Mutator methods a.k.a. setters
In-class work
Classes
- Open up accounts.py. Read through the mostly completed
class definition for a bank account. Using this class will be able
to define Account objects.
- First, we will illustrate the use of several functions. You should
be able to draw a stack diagram for the constructor showing how self
is defined.
- Open up testAccounts.py and trace the code which creates
three separate Account objects using the constructor, prints
them using the string conversion method, and prints out information given
by the accessor methods getBalance and getName
- Take some time to implement and test the changePin method. It
is very important that you employ unit testing just as you did with
the top-down design labs
- When you are finished, analyze and test the withdraw() function.
Once you understand how that works, implement and test the deposit()
method. If you have time, implement and test the computeInterest()
method.
- With the time remaining (possible into next week), we will rethink how
object-oriented programming could improve our Scattergories program from
Week 7. Specifically, we will define a class for creating card objects
that store the categories and answers for a user.
Post-class questions
- when constructing our class object, how do decide what to include
as a parameter?
There is no single rule to answer this. But, in general, you need to think
about how to initialize all of the data in an object. Some of this data
can be a default value, so a parameter is not needed. For example,
we could create an Account object without a balance parameter
and just initialize self.balance to 0. If, however,
some information is needed to initialize the data, we should have a
parameter for it. For example, a Snail object needs to be
centered somewhere in the window. So, we need to send in a parameter for it
- How do you make classes of graphics?
The same as any class. I've released a snowman.py file showing
how you can define a Snowman object for Lab 5. testSnowClass.py uses this class to create an army of snowmen
- Can classes interact?
Yes! You will see this in the next lab,
where a Ball object interacts with a Paddle object. But
you've been doing this for awhile now - GraphWin is an object
and we draw Circles et al in it.
- How to decide what kind of information should be stored in a class
Any data that describes the class/object. What describes an account (name,
id, pin, balance,...)? Your answer
should go in the Account class.
- can you have multiple classes in one progra
Yes! See Lab 10
and also our snowman lab - you drew Circles, Rectangles, etc.
- Can we have a class within a class
Yes! We could
create a Bank class that has a list of Accounts in it
as well as a branch name and address. Also, the Snail class
did this as well.
- What is a class and how does it it make things easier?
A class is a definition or template for creating a specific type of object.
Another way to think of it - it defines a type in Python (like float, int,
string, Circle). It makes things easier by encapsulating a concept - it
puts data and methods that describe a single concept into one container.
- What are some more practical applications of Classes
Every major piece of software uses classes. Think of how you used top-down
design for labs 8 and 9. For real-world applications, we do the same for
very large programs by using classes. Banner (our registration system)
would have classes to define Student, Faculty, Course, Department, Building,
Room, Staff, etc. Spotify probably has classes to describe a Song and
Artist and Album as well as a Playlist (list of Songs with methods like Playlist.shuffle())
- Are there alternatives to self?
In Python? No, not that I'm familiar with. Most languages
use the word this in a similar way.
- What does self mean?
It is a variable for the object being
used. Any data that belongs to the object needs to have the word self before it otherwise it gets lost when the method completes.
- what are the limitations of creating classes to use in functions?
None. I may be misinterpreting this question so please follow up.
- can we go over recursive again, at least before the next quiz?
In class? Probably not. David will have some quiz review in lab next
week, and the ninjas do quiz review anytime they are asked to. Also,
please see me in office hours or by appointment.
include("style/footer.php"); ?>