ppt - Courses

advertisement
INFO 206 Lab Exercise 1
Introduction to Classes and Objects
1/18/2012
i206 Lab 1 - Exercise
1
Reference
Object
• “An object associates data with the particular operations that can use
or affect that data.” ~ Apple
• “Similar to the real-world, objects have both state and behavior.” ~Java
• Objects encapsulate both data and methods that interacts with the
data.
• Objects can store data in fields, also known as instance variables
(attributes)
– Attributes can be a variety of data types
– Attributes can also store references to other objects
• Objects can have methods
• An object’s attributes and methods are defined by its class
1/18/2012
i206 Lab 1 - Exercise
2
Reference
Class:
• The class is the blueprint for creating an object. The
class definition declares the data fields that become
part of every object of the class, and it defines a set of
methods that all objects in the class can use.
• Classes are used to create objects, which are then
called “instances” of that class
• A method is a function that is associated with a
particular class. Unlike a function, a method has to be
called by an instance of the class where the method
was defined.
1/18/2012
i206 Lab 1 - Exercise
3
Lab Exercise
The Bank Account Class
• The object-oriented bank account example is a
common demonstration of OOP by modeling a
bank account class. Let’s see if we can do the
same. We’ll create a program called
PythonBanking.
• Let’s start by launching your favorite IDE for
Python programming.
1/18/2012
i206 Lab 1 - Exercise
4
Lab Exercise - Style Guidelines
1.
The first line of all your python programs must be:
#!/usr/bin/env python
– “#!” a.k.a. shebang indicates that the file is a script
– “/usr/bin/env python” tells the computer to use this interpreter to read this
file
2.
3.
4.
Set your full name in the __author__ special attribute at the top of each
python file
Set your name@ischool.edu email in the __email__ special attribute
Set your python version in the __python_version attribute
– You can determine your python version by typing this in the command line:
>>python –version
5.
__can_anonymously_use_as_example specifies whether or not we can use your
code (or portion of your code) anonymously for example and/or demonstration
1/18/2012
i206 Lab 1 - Exercise
5
Lab Exercise - Style Guidelines
Example header of a python program:
#!/usr/bin/env python
__author__ = 'Alex Chung'
__email__ = 'achung@ischool.berkeley.edu'
__python_version = '2.6.1'
__can_anonymously_use_as_example = True
1/18/2012
i206 Lab 1 - Exercise
6
Lab Exercise - Declaring a Python Class
Before an object can be instantiated we first need to define the “blueprint” for the
object with class definition:
class BankAccount(object):
“””represents a generic banking account”””
This header indicates that the new class is a BankAccount, which inherits from the
generic object class. The body is where we define the class variables and functions
but currently it is empty.
To create a BankAccount object, you call BankAccount like a function:
personalAccount = BankAccount()
The return value is a reference to a BankAccount object. Creating a new object is
called instantiation, and the object is an instance of the class.
1/18/2012
i206 Lab 1 - Exercise
7
Lab Exercise – Defining Class Attributes
Let’s extend our BankAccount class to add class attributes to hold the account name and number.
class BankAccount(object):
“””represents a generic banking account”””
name = “”
accountNumber = “”
balance = 0
We can assign values to the attributes with dot notation:
personalAccount.name = “Alex Chung”
personalAccount.accountNumber = “234324ff3”
personalAccount.balance = 100.0
We can also use the dot notation to access the values of the object’s attributes:
>>>print(personalAccount.name)
Alex Chung
>>>print(personalAccount.accountNumber)
234324ff3
1/18/2012
i206 Lab 1 - Exercise
8
Lab Exercise – Defining Class Attributes
We can also use the init (short for initialization) method that gets invoked
when an object is instantiated:
#inside class BankAccount
def __init__(self, accountName, accountNumber,
accountBalance):
self.name = accountName
self.accountNumber = accountNumber
self.balance = accountBalance
To create a BankAccount object with __init__:
personalAccount = BankAccount(“Alex Chung”,
“234324ff3”, 1000.0)
1/18/2012
i206 Lab 1 - Exercise
9
Lab Exercise – Adding Methods to
Class
Methods are semantically the same as functions, in which, they can take arguments
and return a result. However, methods are defined inside a class definition and they
are called explicitly by the class’ instances.
For example, we want to deposit money into a bank account.
# inside class BankAccount:
def deposit(self, amount):
self.balance = self.balance + amount
Using the earlier personalAccount:
>>>personalAccount = BankAccount(“Alex Chung”,
“234324ff3”, 1000.0)
>>>personalAccount.deposit(100.0)
>>>print(personalAccount.balance)
1100.0
1/18/2012
i206 Lab 1 - Exercise
10
Lab Exercise
Your code should look like this:
#!/usr/bin/env python
__author__ = 'Alex Chung'
__email__ = 'achung@ischool.berkeley.edu'
__python_version = '3.2.2'
__can_anonymously_use_as_example = 'True'
"""OOP Demonstration with Bank Account Program"""
class BankAccount(object):
"""represents a generic banking account"""
def __init__(self, accountName, accountNumber, accountBalance):
self.name = accountName
self.accountNumber = accountNumber
self.balance = accountBalance
def deposit(self, amount):
self.balance = self.balance + amount
personalAccount = BankAccount("Alex Chung", "233444", 1000.0)
print (personalAccount.balance)
personalAccount.deposit(100.0)
print (personalAccount.balance)
1/18/2012
i206 Lab 1 - Exercise
11
A Little Extra
Modify the BankAccount class to help John Doe
figure out his ending balance.
John Doe opens a new savings account with $50.
He earns monthly (every 30 days) 5% interest if
his account has a balance of < $500 and 10% if his
balance exceeds $500. What will his ending
balance be after 6 months if he deposits $200
every 30 days (after his initial deposit of $50 at
day 0).
1/18/2012
i206 Lab 1 - Exercise
12
Download