classes & inheritance review 1. 2 relationships between classes as the building blocks of more...

9
Classes & Inheritance Review 1

Upload: shana-hodges

Post on 13-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

1

Classes & Inheritance Review

Page 2: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

2

Relationships Between ClassesAs the building blocks of more complex systems, objects can be designed to interact with each other in one of three ways:

Association: an object is aware of another object and holds a reference to it

Composition: objects combining to create more complex ones

Inheritance: objects are created as extensions of other objects with additional properties

Page 3: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

3

AssociationIn an associative has-a relationship, an object is aware of another complex object and can communicate with it.

Example: a Car has an owner attribute which is a Person.

Temporary associations can happen through parameters (such as a RentalCar whose drive method takes a Person as a parameter)

Car

owner

Person

name

age

sex

Page 4: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

4

CompositionIn a compositional has-a relationship, an object is made up of less complex objects.

Composition is a kind of association that involves a permanent has-a relationship.

Example: a Movie object is composed of string objects title and genre and integer object year.

Movie

title

genre

year

<str>

<str>

<int>

Page 5: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

5

InheritanceInheritance, as opposed to the previous two examples, is not a has-a relationship. It's an is-a relationship.

It means that objects of a particular class are members of a subset of the objects of another class.

The subclass inherits all the properties of the superclass, and adds some of its own.

Inheritance is a largely misunderstood idea, so we will spend a bit of time clarifying when it is useful and when it is not.

Page 6: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

6

Object Oriented Analysis example

Page 7: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

7

An address book holds a collection of entries. It must be possible to add a new entry to the address book, to edit existing information about an entry and to delete an entry. Entries should keep track of a display name, an address (incl. city, province, etc.) and a primary phone number.Individual entries should include an e-mail address and a Twitter account. Business entries should include a website. An individual's display name is his/her first name, followed by his/her last name. A business's display name is its name.This application should print out full entries with as much information as is available (sorted by type of entry, then by name/last name), as well as create shipping labels.

Page 8: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

8

Memory Model example

from StG midterm 1 (evening section)

Page 9: Classes & Inheritance Review 1. 2 Relationships Between Classes As the building blocks of more complex systems, objects can be designed to interact with

9

class Point(object):

’’’A 2-D Cartesian coordinate.’’’

def __init__(self, x, y):

self.x = x

self.y = y

def are_vertical(pts):

’’’(list of Points) -> bool’’’

if len(pts) <= 1:

# Draw the memory model diagram until you reach here, then stop.

return True

else:

return pts[0].x == pts[1].x and are_vertical(pts[1:])

def use_points():

L = [Point(1, 2)]

assert are_vertical(L)

# Diagram drawn at this point in the execution.

L.append(Point(1, -2))

assert are_vertical(L)

if __name__ == ’__main__’:

use_points()