1 / 9

Classes & Inheritance Review

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

khoi
Download Presentation

Classes & Inheritance Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes & Inheritance Review

  2. 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

  3. 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

  4. 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.

  5. 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.

  6. Object Oriented Analysis example

  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.

  8. Memory Model example from StG midterm 1 (evening section)

  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()

More Related