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

How to do it...

  1. Create a new console application and add a class called SpaceShip to your console application.
        public class SpaceShip 
{

}
  1. Our SpaceShip class will contain a few methods that describe the basics of a spaceship. Go ahead and add these methods to your SpaceShip class:
        public class SpaceShip 
{
public void ControlBridge()
{

}
public void MedicalBay(int patientCapacity)
{

}
public void EngineRoom(int warpDrives)
{

}
public void CrewQuarters(int crewCapacity)
{

}
public void TeleportationRoom()
{

}
}

Because the SpaceShip class forms part of all other intergalactic vessels, it becomes the blueprint for every other vessel.

  1. Next, we want to create a Destroyer class. To accomplish this, we will create a Destroyer class and use a colon after the class name to indicate that we want to inherit from another class (the SpaceShip class). Therefore, the following needs to be added when creating the Destroyer class:
        public class Destroyer : SpaceShip 
{

}

We can also say that the Destroyer class is derived from the SpaceShip class. The SpaceShip class is therefore the base class of all other intergalactic vessels.

  1. Next, add a few methods to the Destroyer class that are unique to a destroyer. These methods belong only to the Destroyer class and not to the SpaceShip class:
        public class Destroyer : SpaceShip 
{
public void WarRoom()
{

}
public void Armory(int payloadCapacity)
{

}

public void WarSpecialists(int activeBattalions)
{

}
}
  1. Finally, create a third class called Annihilator. This is the most powerful intergalactic vessel and is used to wage war on planets. Let the Annihilator class inherit from the Destroyer class by creating the class and marking it derived from the Destroyer class as follows:
        public class Annihilator : Destroyer 
{

}
  1. Finally, add a few methods to the Annihilator class that only belong to this type of SpaceShip class:
        public class Annihilator : Destroyer 
{
public void TractorBeam()
{

}

public void PlanetDestructionCapability()
{

}
}
  1. What we see now is that when we create a new instance of the SpaceShip class in our console application, only the methods defined in that class are available to us. This is because the SpaceShip class does not inherit from any other classes:
  1. Go ahead and create the SpaceShip class with its methods in the console application:
        SpaceShip transporter = new SpaceShip(); 
transporter.ControlBridge();
transporter.CrewQuarters(1500);
transporter.EngineRoom(2);
transporter.MedicalBay(350);
transporter.TeleportationRoom();

You will see that these are the only methods available to us when instantiating a new instance of this class.

  1. Next, create a new instance of the Destroyer class. You will notice that the Destroyer class contains more methods than what we defined when we created the class. This is because the Destroyer class is inheriting the SpaceShip class and therefore inherits the methods of the SpaceShip class:
  1. Go ahead and create the Destroyer class with all its methods in the console application:
        Destroyer warShip = new Destroyer(); 
warShip.Armory(6);
warShip.ControlBridge();
warShip.CrewQuarters(2200);
warShip.EngineRoom(4);
warShip.MedicalBay(800);
warShip.TeleportationRoom();
warShip.WarRoom();
warShip.WarSpecialists(1);
  1. Finally, create a new instance of the Annihilator class. This class contains all the methods of the Destroyer class as well as the methods from the SpaceShip class. This is because Annihilator inherits from the Destroyer, which, in turn, inherits from SpaceShip:
  1. Go ahead and create the Annihilator class with all its methods in the console application:
        Annihilator planetClassDestroyer = new Annihilator(); 
planetClassDestroyer.Armory(12);
planetClassDestroyer.ControlBridge();
planetClassDestroyer.CrewQuarters(4500);
planetClassDestroyer.EngineRoom(7);
planetClassDestroyer.MedicalBay(3500);
planetClassDestroyer.PlanetDestructionCapability();
planetClassDestroyer.TeleportationRoom();
planetClassDestroyer.TractorBeam();
planetClassDestroyer.WarRoom();
planetClassDestroyer.WarSpecialists(3);
主站蜘蛛池模板: 额尔古纳市| 都匀市| 米泉市| 嘉兴市| 临清市| 鹤壁市| 颍上县| 白山市| 榆社县| 鹿泉市| 富锦市| 巩留县| 郑州市| 论坛| 县级市| 驻马店市| 商河县| 阿克苏市| 沭阳县| 梅河口市| 星座| 怀远县| 永靖县| 静乐县| 通江县| 辛集市| 屏南县| 织金县| 松江区| 定边县| 克什克腾旗| 蒲江县| 邻水| 昌宁县| 南涧| 成安县| 白城市| 南投县| 长阳| 基隆市| 临西县|