Difference between revisions of "2022 - ToolCard"
(Created page with "=The Code= <syntaxhighlight lang=c#> class ToolCard : Card { protected string ToolType; protected string Kit; public ToolCard(string t, string k)...") |
(→The Constructors) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | This is a subclass of Card. | ||
+ | |||
=The Code= | =The Code= | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 48: | Line 50: | ||
} | } | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | =New Variables= | ||
+ | The ToolCard has additional variables not found in the parent class. These are 'ToolType' and 'Kit', they are both protected which means they would be available in any subclass of ToolCard. | ||
+ | =The Constructors= | ||
+ | ToolCard has 2 constructor methods, this is okay as long as they have different parameters. These are the constructors: | ||
+ | <syntaxhighlight lang=c#> | ||
+ | public ToolCard(string t, string k) : base() | ||
+ | { | ||
+ | ToolType = t; | ||
+ | Kit = k; | ||
+ | SetScore(); | ||
+ | } | ||
+ | |||
+ | public ToolCard(string t, string k, int cardNo) | ||
+ | { | ||
+ | ToolType = t; | ||
+ | Kit = k; | ||
+ | CardNumber = cardNo; | ||
+ | SetScore(); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | =The | + | So here 1 constructor accepts parameters of 'string t, string k' and the other accepts parameters of 'string t, string k, int cardNo'. |
+ | |||
+ | 'base()' will also run the base constructor method. | ||
+ | |||
+ | =SetScore= | ||
+ | The SetScore method is labelled 'private', therefore this would not be available in any subclass of ToolCard. | ||
+ | |||
+ | =GetDescription= | ||
+ | The GetDescription method will override the method from the Card class. This method would be marked 'virtual' in the Card class. |
Latest revision as of 14:07, 22 December 2021
This is a subclass of Card.
The Code
class ToolCard : Card
{
protected string ToolType;
protected string Kit;
public ToolCard(string t, string k) : base()
{
ToolType = t;
Kit = k;
SetScore();
}
public ToolCard(string t, string k, int cardNo)
{
ToolType = t;
Kit = k;
CardNumber = cardNo;
SetScore();
}
private void SetScore()
{
switch (ToolType)
{
case "K":
{
Score = 3;
break;
}
case "F":
{
Score = 2;
break;
}
case "P":
{
Score = 1;
break;
}
}
}
public override string GetDescription()
{
return ToolType + " " + Kit;
}
}
New Variables
The ToolCard has additional variables not found in the parent class. These are 'ToolType' and 'Kit', they are both protected which means they would be available in any subclass of ToolCard.
The Constructors
ToolCard has 2 constructor methods, this is okay as long as they have different parameters. These are the constructors:
public ToolCard(string t, string k) : base()
{
ToolType = t;
Kit = k;
SetScore();
}
public ToolCard(string t, string k, int cardNo)
{
ToolType = t;
Kit = k;
CardNumber = cardNo;
SetScore();
}
So here 1 constructor accepts parameters of 'string t, string k' and the other accepts parameters of 'string t, string k, int cardNo'.
'base()' will also run the base constructor method.
SetScore
The SetScore method is labelled 'private', therefore this would not be available in any subclass of ToolCard.
GetDescription
The GetDescription method will override the method from the Card class. This method would be marked 'virtual' in the Card class.