- C# 7 and .NET Core 2.0 Blueprints
- Dirk Strauss Jas Rademeyer
- 631字
- 2021-08-27 19:55:27
Classes
The classes used in the Cricket Score Tracker app are then created in the Classes folder. Here you will see a Batsman class and an AllRounder class. For the sake of simplicity, I only created these two classes. In cricket, all bowlers must bat, but not all batsmen have to bowl. You then get bowlers who can bowl and bat equally well, and they are defined as all-rounders. This is what I have modeled here.
Let's have a look at the Batsman class first. We want a batsman to have the abstract properties of a player, but he must also be a batter. Our class, therefore, inherits the Player base class (remember, we can only inherit from a single class) and implements the properties of the IBatter interface:

The class definition, therefore, reads as a Batsman public class, inherits a Player, and implements the IBatter interface. The Batsman class, therefore, looks as follows:
using cricketScoreTrack.BaseClasses; using cricketScoreTrack.Interfaces; namespace cricketScoreTrack.Classes { public class Batsman : Player, IBatter { #region Player public override string FirstName { get; set; } public override string LastName { get; set; } public override int Age { get; set; } public override string Bio { get; set; } #endregion #region IBatsman public int BatsmanRuns { get; set; } public int BatsmanBallsFaced { get; set; } public int BatsmanMatch4s { get; set; } public int BatsmanMatch6s { get; set; } public double BatsmanBattingStrikeRate => (BatsmanRuns * 100)
/ BatsmanBallsFaced; public override int CalculatePlayerRank() { return 0; } #endregion } }
Note that the Batsman class implements the properties of the abstract class and the interface. Also note that, at this point in time, I do not want to add an implementation for the CalculatePlayerRank() method.
Let's have a look at the AllRounder class. We want the all-rounders to also have the abstract properties of a player, but they must also be a batter and a bowler. Our class, therefore, inherits the Player base class but now implements the properties of the IBatter and the IBowler interfaces:

The class definition, therefore, reads as an AllRounder public class, inherits a Player, and implements the IBatter and IBowler interfaces. The AllRounder class, therefore, looks as follows:
using cricketScoreTrack.BaseClasses; using cricketScoreTrack.Interfaces; using System; namespace cricketScoreTrack.Classes { public class AllRounder : Player, IBatter, IBowler { #region enums public enum StrikeRate { Bowling = 0, Batting = 1 } #endregion #region Player public override string FirstName { get; set; } public override string LastName { get; set; } public override int Age { get; set; } public override string Bio { get; set; } #endregion #region IBatsman public int BatsmanRuns { get; set; } public int BatsmanBallsFaced { get; set; } public int BatsmanMatch4s { get; set; } public int BatsmanMatch6s { get; set; } public double BatsmanBattingStrikeRate =>
CalculateStrikeRate(StrikeRate.Batting); #endregion #region IBowler public double BowlerSpeed { get; set; } public string BowlerType { get; set; } public int BowlerBallsBowled { get; set; } public int BowlerMaidens { get; set; } public int BowlerWickets { get; set; } public double BowlerStrikeRate =>
CalculateStrikeRate(StrikeRate.Bowling); public double BowlerEconomy => BowlerRunsConceded /
BowlerOversBowled; public int BowlerRunsConceded { get; set; } public int BowlerOversBowled { get; set; } #endregion private double CalculateStrikeRate(StrikeRate strikeRateType) { switch (strikeRateType) { case StrikeRate.Bowling: return (BowlerBallsBowled / BowlerWickets); case StrikeRate.Batting: return (BatsmanRuns * 100) / BatsmanBallsFaced; default: throw new Exception("Invalid enum"); } } public override int CalculatePlayerRank() { return 0; } } }
You will notice once again that I didn't add in any implementation for the CalculatePlayerRank() method. Because the abstract class defines this method, all classes that inherit from the abstract class must implement this method.
You now also see that this AllRounder class must implement the properties of both IBowler and IBatter.
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- ASP.NET Core 5.0開發入門與實戰
- C#程序設計教程
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- ASP.NET 3.5程序設計與項目實踐
- Go并發編程實戰
- SQL Server數據庫管理與開發兵書
- “笨辦法”學C語言
- Python語言科研繪圖與學術圖表繪制從入門到精通
- Clojure for Java Developers
- Oracle Data Guard 11gR2 Administration Beginner's Guide
- Docker:容器與容器云(第2版)
- 用Python動手學統計學
- jQuery Essentials