90 likes | 209 Views
Classes & Inheritance Review. Relationships Between Classes. As 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
E N D
Relationships Between Classes As 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
Association In 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
Movie title genre year <str> <str> <int> Composition In a compositional has-a relationship, an object is made up of less complex objects. Composition is a kind of association that involves a permanenthas-a relationship. Example: a Movie object is composed of string objects title and genre and integer object year.
Inheritance Inheritance, 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.
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.
Memory Model example from StG midterm 1 (evening section)
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()