Presentation is loading. Please wait.

Presentation is loading. Please wait.

topics object oriented design objects and classes properties

Similar presentations


Presentation on theme: "topics object oriented design objects and classes properties"— Presentation transcript:

1 6001 structure & interpretation of computer programs recitation 13/ november 6, 1997

2 topics object oriented design objects and classes properties
subclassing object oriented coding methods & members inheritance 1/17/2019 daniel jackson

3 modelling the world with objects
idea a good way to structure a program is to mirror the structure of the “real world” first model the real world, then translate into object oriented program elements an object is some entity that has an identity examples: a person, a car, a segment of a road, a file non-examples: an integer, the weather, a friendship, an airline flight a class is a collection of similar objects examples: Person, the class of all persons; Car, the class of all cars 1/17/2019 daniel jackson

4 properties local properties there are properties of individual objects
a person has an age, a social security number a car has a mileage, a speed a file has a size associations and properties of more than one object, sometimes called associations a student is majoring in a course a car is driving on a road segment a file is in a directory 1/17/2019 daniel jackson

5 subclassing definition
if every object of a class C can also be regarded as an object of class D, then C is said to be a subclass of D examples Person is a subclass of Animal Car is a subclass of Vehicle Square is a subclass of Rectangle 1/17/2019 daniel jackson

6 object models idea can express associations and subclasses graphically
use arrow for subclassing and plain line for association drives Vehicle Person Car Truck TruckDriver 1/17/2019 daniel jackson

7 exercise draw an object model for a drawing program
supports variety of lines and shapes, grouping, layers 1/17/2019 daniel jackson

8 object-oriented programming in Java
object has members or instance variables for holding local state methods for reading/writing members class defines methods and members for all objects of the class a constructor for making objects of the class computation model “sending a message to o”: executing o.m (a1, a2, … ) causes object o to execute method m on its members with arguments a1, a2, … this is how other objects access the local state of o inheritance can declare one class to be a subclass of another subclass gets all the methods and members of the superclass, but may redefine them properties are coded as members with associations, have to decide where the state goes 1/17/2019 daniel jackson

9 object-oriented programming in Scheme
OO constructs not built-in, so have to simulate them make inherited members a separate data structure (called a part in lecture) Scheme system doesn’t “understand” objects, so only find errors when the program runs 1/17/2019 daniel jackson

10 get_radius : –> int set_radius : int –> undef
example Shape color : Color area : int draw : Screen –>undef get_color : –> Color set_color : Color –> undef Shape class members color, area, position, boundary methods draw, get_color, set_color Circle class radius set_radius, get_radius Line class length arrow_style move_tail Circle radius : int get_radius : –> int set_radius : int –> undef 1/17/2019 daniel jackson

11 exercise sketch the classes for the drawing program for each class:
members methods 1/17/2019 daniel jackson


Download ppt "topics object oriented design objects and classes properties"

Similar presentations


Ads by Google