tags | languages |
---|---|
tutorial, beginner, OOP, Object-Oriented Programming, model |
objc |
- Solidify understanding of objects and object-oriented programming
- Begin learning efficient class design via inheritance
-
Create three classes: Card, PlayingCard, and BaseballCard.
Card
will be our base class, and should inherit fromNSObject
.PlayingCard
andBaseballCard
should each inherit fromCard
. (Shortcut-time! Hit cmd+N, then select 'Cocoa Touch Class') -
Cards have many distinguishing characteristics or (hint) properties. Flesh out your classes using this list. Think about which ones belong to all
Card
s, which belong to aPlayingCard
, and which belong to aBaseballCard
.- playerWeight (Integer) - The player's weight in pounds
- suit (String) - The card's suit
- playerNumber (Integer) - The player's number
- rank (Integer) - The cards rank. ex. 4 of clubs has a rank of 4
- playerHeight (Integer) - The player's height in inches
- teamName (String) - The player's team's name
- cardNumber (Integer) - The card's production number ie. 279
- playerFirstName (String) - The first name of the player on the card
- playerLastName (String) - The last name of the player on the card
- brand (String) - The brand of the card
Note: it's okay if your
Card
class seems a little bare — if applicable, having a basic parent class can be a good design choice because it can make future revisions easier. -
You may have noticed that many of our properties describe characteristics of a baseball player... Imagine we have two different
BaseballCard
s that each feature the same baseball player. Instead of retyping all of the players information, wouldn't it be nice if we could reuse a player we've already entered? -
Let's refactor our code to be more re-usable by creating a fourth class called
BaseballPlayer
.- Move all of the properties related to a baseball player out of the
BaseballCard
class and into your newly createdBaseballPlayer
class. - Don't forget to make the
BaseballPlayer
's properties public! Make sure they're in the class's@interface
in its .h file. - Create a property of type
BaseballPlayer
in theBaseballCard
class. [Pro-tip: if the compiler is complaining, you likely forgot to#import "BaseballPlayer.h";
]
- Move all of the properties related to a baseball player out of the
-
Override the
description
method forPlayingCard
andBaseballCard
classes.-
for
PlayingCard
: should return a string containing itsrank
andsuit
(copy/paste these ASCII representations for the suits: ♥ ♠ ♣ ♦). -
for
BaseballCard
: should return a string containing the playersfirstName
,lastName
, andteamName
.
-
-
Time to see them in action! Inside of
AppDelegate.m
'sdidFinishLaunchingWithOptions
, instantiate 3PlayingCard
s, 3baseballCard
s and 2BaseballPlayer
s. 2 of yourBaseballCard
s should share the sameBaseballPlayer
. -
Print the
description
of yourBaseballCard
s andPlayingCard
s to the console usingNSLog
.
Be sure to run the tests too!
- Add designated initializers to
PlayingCard
,BaseballPlayer
andBaseballCard
classes, and refactor your initializations (indidFinishLaunchingWithOptions
) to use your new designated initializers. - Create methods in the
BaseballPlayer
class that return weight in KG (NSNumber
) and height in feet and inches (NSString
)