fall 2007acs-1805 ron mcfadyen1 chapter 4 oo programming concepts

22
Fall 2007 ACS-1805 Ron McFadyen 1 Chapter 4 OO Programming Concepts

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Fall 2007 ACS-1805 Ron McFadyen 1

Chapter 4

OO Programming Concepts

Fall 2007 ACS-1805 Ron McFadyen 2

Elements of Programs

Chapter 4 introduces more advanced OO programming techniques.

Construction of a programs usually requires:

•Classes

•Objects

•Methods

•World-level

•Class-level

•Parameters

•Inheritance

Fall 2007 ACS-1805 Ron McFadyen 3

Classes An OO program is organized around the concept of class.For Alice programming, all classes are pre-defined for us.We choose the classes our program requires from the gallery.We need other skills to build classes

If interested, you would want to learn about products such as Maya, Max Studio, …

• These products are used to construct 3D models• Constructing or building classes is outside the scope of this

course

Two things that distinguish classes:• Classes are defined to have properties and methods (including

functions)

Fall 2007 ACS-1805 Ron McFadyen 4

Classes Alice worlds are populated by selecting objects from a gallery

of classes

Fall 2007 ACS-1805 Ron McFadyen 5

Classes Each class has properties, methods, functions

We can customize by creating new properties, methods, functions

Fall 2007 ACS-1805 Ron McFadyen 6

Objects

We instantiate objects when we “add objects” from the gallery

Fall 2007 ACS-1805 Ron McFadyen 7

Objects We instantiate objects when we “add objects” from the gallery

Fall 2007 ACS-1805 Ron McFadyen 8

Objects Showing the relationship amongst classes and objects:

Teacher

teacher2 teacher1

Student

student2

student1

student2

Fall 2007 ACS-1805 Ron McFadyen 9

Methods

Each class/object has a collection of methods that define the things an object from that class can do

• Move, Turn, Roll, etc

• We can’t see the code comprising these. (The Alice creators don’t want us to change them and they are referred to as primitive methods

We can customize a object by giving it some new methods

• you are able to edit those

OO programmers typically use many many methods where each is defined to some fairly simple thing

Methods give us a way of organizing the complexity of our creations

Methods become even more useful when they incorporate parameters

Fall 2007 ACS-1805 Ron McFadyen 10

World-level MethodsInstead of one complex method, we can break up a large complex task into smaller simpler ones.

The main idea is to take a group of related instructions in one method, place them in a separate method, and replace the original lines with a call to the new method

The principle we are applying is called abstraction – we are now thinking of the group of instructions as a single instruction

Fall 2007 ACS-1805 Ron McFadyen 11

World-level MethodsOriginal: Code is placed in a new method:

Fall 2007 ACS-1805 Ron McFadyen 12

World-level MethodsOriginal: Revised:

• Original is long and complex

• The revised version is easily understood

• Note how a method is called

• Note use of comments

Fall 2007 ACS-1805 Ron McFadyen 13

ParametersA method is more useful if it can work for different objects

• A robot walks by turning a leg part backward and then forward

• We can place these two turns into a method with a parameter – where the parameter is used to specify the part involved

Fall 2007 ACS-1805 Ron McFadyen 14

Parameters

A method with a parameter:

Some original code:

Fall 2007 ACS-1805 Ron McFadyen 15

Parameters

Revised:

Original:

parameters

What different kinds of parameters does Alice allow?

Fall 2007 ACS-1805 Ron McFadyen 16

Class-level Methods

Any object can be extended to include new methods of your choosing

e.g. your people object can be specialized to include a walk method.

Fall 2007 ACS-1805 Ron McFadyen 17

Class-level Methods

After customizing an object you can save the object to a class from which you can instantiate (as from the gallery).

If you customize alienRobot to include its own walk method and save it as a class named myRobot, then it has all the original properties, methods, functions of alienRobot and any new ones you define.

We say that myRobot inherits the properties, methods, functions of alienRobot

Fall 2007 ACS-1805 Ron McFadyen 18

Class-level Methods

An object can be saved as a class …

and later on imported to instantiate an object in another world

An object instantiated from myRobot inherits the properties, methods, functions of myRobot

Fall 2007 ACS-1805 Ron McFadyen 19

Class-level Methods

Recommendations:

•Use many class-level methods

•Examine Lion, He/She builders for class-level methods

•Avoid references to world methods from class-level methods

•Avoid direct references to objects from within a class-level method – use parameters instead

Fall 2007 ACS-1805 Ron McFadyen 20

Tips/TechniquesSome properties of objects and world:

opacity: The opacity of an object is a measure of how difficult or easy it is to see through an object.

Fall 2007 ACS-1805 Ron McFadyen 21

Tips/TechniquesSome properties of objects and world:

isShowing: This property is either true or false and determines, respectively, if the object is visible or invisible.

Whenever you add an object to a world it is initially visible.

Fall 2007 ACS-1805 Ron McFadyen 22

Rotating Objects Example

Consider two objects that are to move, circling each other. Similar to the text example, commanding each to turn as seen by the other results in an odd result.

But commanding each to turn about some invisible object does work.

See demo