官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 汤阴县| 广丰县| 黄陵县| 信丰县| 招远市| 邢台县| 贺兰县| 弥渡县| 湘西| 宝山区| 镇安县| 澄迈县| 丰顺县| 陆川县| 曲阳县| 英德市| 沙田区| 龙里县| 南涧| 博客| 清河县| 长兴县| 剑河县| 崇文区| 淳化县| 平原县| 沙坪坝区| 娄底市| 石棉县| 曲麻莱县| 思茅市| 德格县| 汝城县| 宁海县| 绍兴市| 祁连县| 亚东县| 阳西县| 平顶山市| 新化县| 大渡口区|