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

How to do it...

  1. Create an abstract class called Shuttle and give it a member called TWR, which is the calculation of the TWR of the shuttle:
        public abstract class Shuttle 
{
public abstract double TWR();
}
  1. Next, create a class called NasaShuttle and have it inherit from the abstract class Shuttle by putting the abstract class name after a colon at the end of the NasaShuttle class declaration:
        public class NasaShuttle : Shuttle 
{

}
  1. Visual Studio will underline the NasaShuttle class because you have told the compiler that the class inherits from an abstract class, but you have not yet implemented the members of that abstract class:
  1. 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. Visual Studio then adds the missing implementation to your NasaShuttle class. By default, it will add it as not implemented, because you are required to provide implementation for the abstract member you overrode in the abstract class:
        public class NasaShuttle : Shuttle 
{
public override double TWR()
{
throw new NotImplementedException();
}
}
  1. Create another class called RoscosmosShuttle and inherit from the same Shuttle abstract class:
        public class RoscosmosShuttle : Shuttle 
{

}
  1. As before, Visual Studio will underline the RoscosmosShuttle class because you have told the compiler that the class inherits from an abstract class, but you have not yet implemented the members of that abstract class.
  1. 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. The overridden method is then added to the RoscosmosShuttle class as not implemented. You have just seen an example of dynamic polymorphism in action:
        public class RoscosmosShuttle : Shuttle 
{
public override double TWR()
{
throw new NotImplementedException();
}
}
  1. To see an example of static polymorphism, create the following overloaded constructor for NasaShuttle. The constructor name stays the same, but the signature of the constructor changes, which makes it overloaded:
        public NasaShuttle(double engineThrust, 
double totalShuttleMass, double gravitationalAcceleration)
{

}

public NasaShuttle(double engineThrust,
double totalShuttleMass, double planetMass,
double planetRadius)
{

}
主站蜘蛛池模板: 湛江市| 桃园县| 徐水县| 睢宁县| 固阳县| 武清区| 延寿县| 宝丰县| 肇州县| 新巴尔虎右旗| 哈巴河县| 茌平县| 齐河县| 青阳县| 祁阳县| 四川省| 弥勒县| 延川县| 兰坪| 普格县| 小金县| 繁峙县| 额尔古纳市| 炉霍县| 巴马| 曲靖市| 六枝特区| 阳西县| 海伦市| 大冶市| 孝义市| 衡山县| 隆安县| 桦甸市| 吉安市| 肥乡县| 池州市| 武强县| 桓台县| 即墨市| 墨江|