Game Development Reference
In-Depth Information
Red,
Black
}
enum Sex
{
Male,
Female
}
class Baby
{
public string Name { get; set; }
public Sex Sex { get; set; }
public HairColor HairColor { get; set; }
}
class Player
{
public Baby HaveBaby(string babyName, Sex sex
¼
Sex.Female, HairColor
hairColor
¼
HairColor.Black)
{
return new Baby()
{
Name
¼
babyName,
Sex
¼
sex,
HairColor
¼
hairColor
};
}
}
Player player
¼
new Player();
player.HaveBaby(''Bob'', Sex.Male);
player.HaveBaby(''Alice'');
In this example, the player class has code that allows the player to give birth.
The
HaveBaby
method takes in a name and some other optional arguments
and then returns a new baby object. The
HaveBaby
method requires a name to
be given but the
sex
and
hairColor
arguments are optional. The next ex-
ample shows how to set the hair color argument but leave the
sex
as the default
female
value.
player.HaveBaby(''Jane'', hairColor: HairColor.Blonde)
Named arguments provide a way to only set the arguments we want to set.
Search WWH ::
Custom Search