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

Abstract classes

Open up the BaseClasses folder and double click on the Player.cs file. You will see the following code:

namespace cricketScoreTrack.BaseClasses 
{ 
    public abstract class Player 
    { 
        public abstract string FirstName { get; set; } 
        public abstract string LastName { get; set; } 
        public abstract int Age { get; set; } 
        public abstract string Bio { get; set; } 
    } 
} 

This is our abstract class. The abstract modifier in the class declaration and the properties tells us that this thing we are going to modify has missing or incomplete implementation. It, therefore, is only intended for use as a base class. Any member marked as abstract must be implemented by classes that are derived from our Player abstract class.

The abstract modifier is used with:

  • Classes
  • Methods
  • Properties
  • Indexers
  • Events

If we had to include a method called CalculatePlayerRank() in our abstract Player class, then we would need to provide an implementation of this method in any class that is derived from Player.

Therefore, in the Player abstract class, this method would be defined as follows:

abstract public int CalculatePlayerRank(); 

In any derived classes, Visual Studio 2017 will be running code analyzers to determine if all the members of the abstract class have been implemented by the derived classes. When you let Visual Studio 2017 implement the abstract class in a derived class, it is defaulted with NotImplementedException() in the method body:

public override int CalculatePlayerRank() 
{ 
  throw new NotImplementedException(); 
} 

This is done because you haven't actually provided any implementation for the CalculatePlayerRank() method yet. To do this, you need to replace throw new NotImplementedException(); with actual working code to calculate the rank of the current player.

It is interesting to note is that while NotImplementedException() is within the body of the CalculatePlayerRank() method, it does not warn you that the method isn't returning an int value.

Abstract classes can be seen as a blueprint of what needs to be done. The way you do it is up to you as a developer.

主站蜘蛛池模板: 广宁县| 景洪市| 安泽县| 留坝县| 仪征市| 蒲城县| 唐海县| 治县。| 云梦县| 台湾省| 大厂| 四川省| 邹平县| 上杭县| 文成县| 曲麻莱县| 镇赉县| 同仁县| 新余市| 花莲市| 丹江口市| 特克斯县| 池州市| 伊宁县| 辽阳县| 伊川县| 湘乡市| 柞水县| 无锡市| 商水县| 西林县| 龙口市| 宜章县| 邓州市| 桂平市| 民权县| 通城县| 衡阳县| 南丰县| 上饶县| 当涂县|