9
Object-Oriented Programming and Classes Chapter 7

Chapter 07

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Chapter 07

Object-Oriented Programming and Classes

Chapter 7

Page 2: Chapter 07

7

RefactoringClasses should perform one task.

If a class performs multiple tasks, it should be refactored into two or more classes.

This process is analogous to factoring composite numbers into their primes.

Page 3: Chapter 07

7

Setting StateAn object uses member variables to keep track of its own state.

The Ball object knows its own:x and y location

x and y velocity

Page 4: Chapter 07

7

Implementing MethodsThe Ball class is responsible for:

Moving itselfDrawing itself

The World does the following:Erases the ballMoves the ballChecks for ball collision and changes the ball velocity, if necessaryDraws the ball

Page 5: Chapter 07

7

Encapsulating Responsibility

A class should be responsible for its own behavior.

Encapsulation makes programs easier to maintain and expand.

It is easier to reuse encapsulated classes in other programs.

Page 6: Chapter 07

7

Program Design Considerations

Good software design requires planning, evaluation, and conscious implementation decisions.

Symptoms of poor program design: Many small, data-only objects and one large, controlling object that makes everything happen

Objects that need to know a lot of details about another object’s implementation

Page 7: Chapter 07

7

Method OverloadingCreating more than one version of a method with different parameter lists

Compiler decides which method to use based on method signature.

Number of parameters

Order of parameters

Type of parameters

Overloading provides “optional” arguments.

Page 8: Chapter 07

7

Overloading ConstructorsConstructor methods are used when an object is instantiated.

Overloaded constructors provide optional ways of creating objects.

Page 9: Chapter 07

7

Putting It All TogetherBall class overloaded to permit clients to create different size Ball objects

HWall class overloaded to allow clients to specify elasticity