Upload
graham-royce
View
45
Download
5
Embed Size (px)
DESCRIPTION
Citation preview
Object-Oriented Programming and Classes
Chapter 7
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.
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
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
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.
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
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.
7
Overloading ConstructorsConstructor methods are used when an object is instantiated.
Overloaded constructors provide optional ways of creating objects.
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