object oriented programing (oop) intro2cs – week 7b 1

38
Object Oriented Programing (OOP) Intro2CS – week 7b 1

Upload: reynard-greene

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programing (OOP) Intro2CS – week 7b 1

1

Object Oriented Programing(OOP)

Intro2CS – week 7b

Page 2: Object Oriented Programing (OOP) Intro2CS – week 7b 1

2

What is Object Oriented Programing?

• A programing paradigm that is based on data, and code that are coupled together.

• Objects store information and carry the code that accesses and manipulates information.

Our first example will be the Car class.• The class Car is the type.• Specific cars of that class are objects (instances).

Page 3: Object Oriented Programing (OOP) Intro2CS – week 7b 1

3

How “Car” will be used

Things to notice:• When we “use” the car, it changes its internal state (millage increases, gas is

consumed)• Cars “remember” things about their current state, • have functions (methods) that can change that state.

• Each car is separate.

Create a new car

and another…

Do things with it

Get information about it

Page 4: Object Oriented Programing (OOP) Intro2CS – week 7b 1

4

Class vs Object

Additional statements will come here.

Defines a new class named Car.Class names should have capital letters in the beginning of each word. examples:: Car, BigNumber, ChessBoard

Create two instances (objects) of type Car. Notice the parentheses: ()

Page 5: Object Oriented Programing (OOP) Intro2CS – week 7b 1

5

• To check if an object is of a certain instance:

This is usually not good code.

Page 6: Object Oriented Programing (OOP) Intro2CS – week 7b 1

6

Class vs Object

The class “Car”: <class '__main__.Car'>

Add a field to each object noting the amount of fuel in the car. Each object has its own value.These are called by many names: member variables, member fields, attributes

Page 7: Object Oriented Programing (OOP) Intro2CS – week 7b 1

7

Class vs Object

A class variable. Shared by all objects.

Output:

Page 8: Object Oriented Programing (OOP) Intro2CS – week 7b 1

8

Self is the newly created object

Initializing objects

Output:

The init method. Runs whenever a new object is created. Sometimes called a constructor

Here is the argument given to __init__.

Page 9: Object Oriented Programing (OOP) Intro2CS – week 7b 1

9

Initializing objects

Output:

Variables within the class definition are class variables.

Page 10: Object Oriented Programing (OOP) Intro2CS – week 7b 1

10

Objects Have State• The data inside objects represents their state

• The state is changed by performing actions on the object: Invoking “methods”

(methods are functions that run and may change its state)

Page 11: Object Oriented Programing (OOP) Intro2CS – week 7b 1

11

MethodsFunctions within the class definition are called methods, or member functions. They add functionality to the objects.

Here is how they are invoked.

The first parameter is usually called self. It’s the object they run on

Page 12: Object Oriented Programing (OOP) Intro2CS – week 7b 1

12

These two calls are equivalent

Page 13: Object Oriented Programing (OOP) Intro2CS – week 7b 1

13

Hiding the internal representation• Good object oriented programming helps objects

behave in a consistent manner

• Example: No way to increase the mileage of the car without driving it (and burning fuel)

• If the programmer directly changes car1.mileage=-2 he can make mistakes.

• It is therefore good practice to write objects that hide internal data and only change as a result of their own method calls.

Page 14: Object Oriented Programing (OOP) Intro2CS – week 7b 1

14

Hiding the internal representation

Will not work. Variable is still there, but has actually been renamed to _Car__fuel

Private members are denoted with a double underscore.

access data only through methods.

Page 15: Object Oriented Programing (OOP) Intro2CS – week 7b 1

15

Hiding the Internals• If the class is written properly, it will be

impossible to change the internal state to something inconsistent.

• A car with negative fuel?• A car with negative milage?

Page 16: Object Oriented Programing (OOP) Intro2CS – week 7b 1

16

Ensuring internal consistency

Similarly with get_milage

Page 17: Object Oriented Programing (OOP) Intro2CS – week 7b 1

17

Application Interfaces (APIs) and Design by Contract

• The methods of the class define its API – how programmers interact with its instances

• The “design by contract” paradigm: – APIs make promises about the behavior of objects

(but without committing to internal representation). – Encapsulation: Programmer needs only the

interface, doesn’t care about internals.

Page 18: Object Oriented Programing (OOP) Intro2CS – week 7b 1

18

All Things are Objects

Page 19: Object Oriented Programing (OOP) Intro2CS – week 7b 1

19

PolymorphismActions can sometimes be applied to objects of many different types. This is very natural in Python.

Page 20: Object Oriented Programing (OOP) Intro2CS – week 7b 1

20

PolymorphismActions can sometimes be applied to objects of many different types. This is very natural in Python.

(There are actually better ways to do this in OOP using inheritance)

Page 21: Object Oriented Programing (OOP) Intro2CS – week 7b 1

21

Documenting classes• Write docstrings for each method in the class.

– Write what the API promises regarding each method

• Write a docstring at the beginning of the class– Here you should also document attributes, but only the public ones

Page 22: Object Oriented Programing (OOP) Intro2CS – week 7b 1

22

Objects are Mutable

No “return” of car object. What happens?

Page 23: Object Oriented Programing (OOP) Intro2CS – week 7b 1

23

Another Example

Page 24: Object Oriented Programing (OOP) Intro2CS – week 7b 1

24

Page 25: Object Oriented Programing (OOP) Intro2CS – week 7b 1

25

• Now we can work directly with the point and rectangle classes.

Page 26: Object Oriented Programing (OOP) Intro2CS – week 7b 1

26

Adding more behavior to objects

We’d like something nicer to be printed

Page 27: Object Oriented Programing (OOP) Intro2CS – week 7b 1

27

The __str__ method is called when a conversion to a string is needed.

Page 28: Object Oriented Programing (OOP) Intro2CS – week 7b 1

28

Defining how operators behave

The default behavior of == on objects it to check if the are the exact same object, ignoring data members.

False

Page 29: Object Oriented Programing (OOP) Intro2CS – week 7b 1

29

• To redefine how == behaves, simply implement the method __eq__() within the class.

Page 30: Object Oriented Programing (OOP) Intro2CS – week 7b 1

30

Other operators• We can define other magic methods in python

that make other operators work with our code

There are more…

+ object.__add__(self, other)- object.__sub__(self, other)* object.__mul__(self, other)// object.__floordiv__(self, other)/ object.__div__(self, other)% object.__mod__(self, other)** object.__pow__(self, other[, modulo])<< object.__lshift__(self, other)>> object.__rshift__(self, other)& object.__and__(self, other)^ object.__xor__(self, other)| object.__or__(self, other)

< object.__lt__(self, other)<= object.__le__(self, other)== object.__eq__(self, other)!= object.__ne__(self, other)>= object.__ge__(self, other)> object.__gt__(self, other)

Page 31: Object Oriented Programing (OOP) Intro2CS – week 7b 1

31

An example• Python has a set object (a container that

doesn’t allow items to be included twice)• But what if it didn’t?• How would we implement one?

Page 32: Object Oriented Programing (OOP) Intro2CS – week 7b 1

32

This implementation is inefficient. (Why?)How can we make it better using things we’ve seen?

Page 33: Object Oriented Programing (OOP) Intro2CS – week 7b 1

33

Gets the “in” operator to work

Gets the == operator to work

Gets the + operator to work

Gets the str() conversion to work

Gets the len() function to work

Page 34: Object Oriented Programing (OOP) Intro2CS – week 7b 1

34

Page 35: Object Oriented Programing (OOP) Intro2CS – week 7b 1

35

Another example

Page 36: Object Oriented Programing (OOP) Intro2CS – week 7b 1

36

What assumptions are we making about items we add in?

Page 37: Object Oriented Programing (OOP) Intro2CS – week 7b 1

37

Page 38: Object Oriented Programing (OOP) Intro2CS – week 7b 1

38