- C# 7 and .NET Core 2.0 Blueprints
- Dirk Strauss Jas Rademeyer
- 381字
- 2025-02-25 23:58:46
Interfaces
Open up the Interfaces folder and have a look at the IBatter.cs and IBowler.cs files. The IBatter interface looks as follows:
namespace cricketScoreTrack.Interfaces { interface IBatter { int BatsmanRuns { get; set; } int BatsmanBallsFaced { get; set; } int BatsmanMatch4s { get; set; } int BatsmanMatch6s { get; set; } double BatsmanBattingStrikeRate { get; } } }
Looking at the IBowler interface, you will see the following:
namespace cricketScoreTrack.Interfaces { interface IBowler { double BowlerSpeed { get; set; } string BowlerType { get; set; } int BowlerBallsBowled { get; set; } int BowlerMaidens { get; set; } int BowlerWickets { get; set; } double BowlerStrikeRate { get; } double BowlerEconomy { get; } int BowlerRunsConceded { get; set; } int BowlerOversBowled { get; set; } } }
An interface will only contain the signatures of methods, properties, events, or indexers. If we had to add a method to the interface to calculate the spin of the ball, it would look something like this:
void CalculateBallSpin();
On the implementation, we would see the code implemented as follows:
void CalculateBallSpin()
{
}
The next logical question would probably be what the difference is between an abstract class and an interface. Let's turn to the excellent Microsoft documentation at—https://docs.microsoft.com/en-us/.
Microsoft sums up an interface very nicely with the following statement:
Think of interfaces as verbs; that is to say, interfaces describe some sort of action. Something that a cricket player does. In this case, the actions are batting and bowling. The interfaces in Cricket Score Tracker are therefore IBatter and IBowler. Note that convention dictates that interfaces begin with the letter I.
Abstract classes on the other hand, act as a noun that tells you what something is. We have Batsmen and All-Rounders. We can say that both these cricketers are players. That is the common noun that describes the cricketers in a cricket match. Therefore, the Player abstract class makes sense here.