- C# 7 and .NET Core 2.0 Blueprints
- Dirk Strauss Jas Rademeyer
- 381字
- 2021-08-27 19:55:26
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.
- Java高并發核心編程(卷2):多線程、鎖、JMM、JUC、高并發設計模式
- Mastering Selenium WebDriver
- FreeSWITCH 1.6 Cookbook
- 假如C語言是我發明的:講給孩子聽的大師編程課
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第2版)
- Python Data Analysis(Second Edition)
- Data Analysis with Stata
- Scala for Machine Learning(Second Edition)
- Web性能實戰
- Java7程序設計入門經典
- 交互設計師成長手冊:從零開始學交互
- 菜鳥成長之路
- LibGDX Game Development By Example
- jQuery EasyUI從零開始學
- 亮劍C#項目開發案例導航