Class - Object - instance-of relationship - type
encapsulation
data hiding - abstraction
message - method (instance vs class method)
argument - return value
data value (instance vs class)
variable - constant
inheritance - superclass - subclass
What are the different components of the game?
What does the player need to know? (information/state)
What does the player need to do? (operations/methods/actions/procedures)
What does the player need to communicate to the other components? (Messages)
What do other components need to communicate a player? (Messages)
Define interface to the outside world that hides all the gory details.
For example:
- The player can ask the pair of dice to roll and to report the result back to the player. Player does care how the pair of dice achieves its roll as long as it does it correctly.
- The game can ask the deck of cards to shuffle itself.
- The player might ask the deck of cards to deal it a card.
- The game can ask the play to take its turn.
There may be many players in a game of monopoly. Each player is the same in that each must store a value for its location, money, etc. However, the specific value will be different for each player. The class defines the structure of the player. Each player object will have that structure but the values stored in that structure will be different.
The class defines the type.
We say that Tabby and Scooby are objects of type Player.
The information an object knows (data) is stored in memory. We give the memory
locations names so that we can refer to them. It is important to differentiate
between the
comChest = 2
numDice = 2
Both have the same value but represent different data.
At a later time, we might set
comChest = 4
The location comChest now has a different value than it had before.
Data can be constant meaning it's value is set once and never changes. For example, the position of the "go to jail" square is always square 10. Thus we might make the data item called jail to be constant:
jailPosition = 10 (we'll make this a constant)
On the other hand, the position of the player changes over the course of the game so we store a player's current position in something called a variable:
playerPosition = 5 (we'll make this a variable)
The name of the memory location stays the same but the value of playerPosition will change over the course of the game.
In the above picture, the message roll(2) is said to have one argument whose value is 2.
When designing a program, we plan out what classes we using diagrams:
Each player has a board position (playerPosition) that will be different from the positions of all of the other players. However, we may also want each player to keep track of whose turn it currently is (whoseTurn). While the value of whoseTurn will change over the course of the game, its value should be the same for all players at any instant in time. We can define
instance variable: playerPosition
class variable: whoseTurn
A class variable is defined once for all objects of a given class whereas an instance variable is defined separately for each object in the class.
Methods can also be instance or class. We'll see how these work a little
later.