Software Development (CSC 321 2016F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [Teaching & Learning]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Outlines]
Reference: [Slack] [Ruby@CodeCademy]
Related Courses: [Rails Tutorial] [CSC 321 2016S @ EdX] [CSC 321 2015S (Davis)] [CSC 321 2016S (Rebelsky)] [CSC 322 2016F (Rebelsky)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
Example: Player class
But you can't change the Player.
public class Player
{
String name;
int hp;
Object[] bag;
int numPossessions;
void takeDamage(int amt)
{
...
} // takeDamage
void pickUp(Object thing)
{
bag[numPossessions++] = thing;
} // pickUp(Object)
} // class Player
public class Dragon extends Player
{
int pilesOfGold;
void breatheFire(Player target)
{
// target.takeDamage(5000 + random(pilesOfGold)*50);
target.hp -= (BASE_FIRE_DAMAGE + random(pilesOfGold)*GOLD_FIRE_MODIFIER);
}
@Override
void takeDamage(int amt)
{
super.takeDamage(amt/10);
}
@Override
void pickup(Object thing)
{
throw new StupidActionException("Dragons cannot pick up things.");
} // pickup(Object)
} // class Dragon
Problems with modified breatheFire - Why is it bad to access
target.hp but okay to call target.takeDamage.
Player changes? We hope that a change to Player
that does not affect the behavior of Player should not affect
Dragon.public, private, protected,
no modifier (package)public: Any other object can access this.private: Only objects in this class can access this.protected: Only (a) objects in this class and subclasses and
(b) objects in the same package.private for all fields, and public for
methods as appropriate.