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

How to do it...

  1. Create an abstract class called SpaceCadet. This is the first type of astronaut you can get when starting with training. The abstract class and its members are defined using the abstract keyword. A thing to note is that abstract classes cannot be instantiated. The members represent the skills that SpaceCadet will have, such as negotiation and basic weapons training:
        public abstract class SpaceCadet 
{
public abstract void ChartingStarMaps();
public abstract void BasicCommunicationSkill();
public abstract void BasicWeaponsTraining();
public abstract void Negotiation();
}
  1. Next, create another abstract class called SpacePrivate. This abstract class inherits from the SpaceCadet abstract class. What we are basically saying is that when a space cadet is trained as a space private, they will still have all the skills learned as a space cadet:
        public abstract class SpacePrivate : SpaceCadet 
{
public abstract void AdvancedCommunicationSkill();
public abstract void AdvancedWeaponsTraining();
public abstract void Persuader();
}
  1. To demonstrate this, create a class called LabResearcher and inherit the SpaceCadet abstract class. Inheriting from the abstract class is done by defining a colon and abstract class name after the newly created class name. This tells the compiler that the LabResearcher class inherits from the SpaceCadet class:
        public class LabResearcher : SpaceCadet 
{

}

Because we are inheriting an abstract class, the compiler will underline the LabResearcher class name to warn us that the derived class does not implement any of the methods in the SpaceCadet abstract class.

  1. If you hover your mouse over the squiggly line, you will see that the lightbulb tip provides us with the issues discovered:
  1. Visual Studio does a great job of providing a solution to the issues discovered. By typing Ctrl . (control key and period), you can let Visual Studio show you some potential fixes (in this case, only one fix) for the issues identified:
  1. After Visual Studio has added the required methods, you will see that these are the same methods defined in the SpaceCadet abstract class. Abstract classes, therefore, require the classes inheriting from the abstract class to implement the methods defined in the abstract class. You will also notice that the methods added to the LabResearcher class contain no implementation and will throw an exception if used as is:
        public class LabResearcher : SpaceCadet 
{
public override void BasicCommunicationSkill()
{
thrownewNotImplementedException();
}

publicoverridevoid BasicWeaponsTraining()
{
thrownewNotImplementedException();
}

publicoverridevoid ChartingStarMaps()
{
thrownewNotImplementedException();
}

publicoverridevoid Negotiation()
{
thrownewNotImplementedException();
}
}
  1. Next, create a class called PlanetExplorer and make this class inherit from the SpacePrivate abstract class. You will remember that the SpacePrivate abstract class inherited from the SpaceCadet abstract class:
        public class PlanetExplorer : SpacePrivate 
{

}
  1. Visual Studio will once again warn you that your new class does not implement the methods of the abstract class that you are inheriting from. Here, however, you will notice that the lightbulb tip informs you that you are not implementing any of the methods in the SpacePrivate and SpaceCadet abstract classes. This is because the SpacePrivate abstract class is inheriting from the SpaceCadet abstract class:
  1. As before, to fix the issues identified, type Ctrl . (control key and period) and let Visual Studio show you some potential fixes (in this case, only one fix) for the issues identified.
  1. After the fixes have been added to your code, you will see that the PlanetExplorer class contains all the methods in the SpacePrivate and SpaceCadet abstract classes:
        public class PlanetExplorer : SpacePrivate 
{
public override void AdvancedCommunicationSkill()
{
throw new NotImplementedException();
}

public override void AdvancedWeaponsTraining()
{
throw new NotImplementedException();
}

public override void BasicCommunicationSkill()
{
throw new NotImplementedException();
}

public override void BasicWeaponsTraining()
{
throw new NotImplementedException();
}

public override void ChartingStarMaps()
{
throw new NotImplementedException();
}

public override void Negotiation()
{
throw new NotImplementedException();
}

public override void Persuader()
{
throw new NotImplementedException();
}
}
主站蜘蛛池模板: 乐昌市| 冀州市| 灵武市| 伊川县| 宿松县| 遂川县| 灵丘县| 枣强县| 万山特区| 永仁县| 渭源县| 玉门市| 武穴市| 华安县| 逊克县| 和田市| 太康县| 霸州市| 龙川县| 靖安县| 江孜县| 伊金霍洛旗| 韶山市| 庆安县| 梅州市| 永登县| 五大连池市| 家居| 西昌市| 繁昌县| 安康市| 涞源县| 新河县| 宜都市| 南木林县| 南皮县| 北川| 玉环县| 恩施市| 建德市| 绥化市|