- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 376字
- 2021-07-03 00:12:03
How to do it...
- 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();
}
- 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
{
}
- 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:

- 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:

- 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();
}
}
- Create another class called RoscosmosShuttle and inherit from the same Shuttle abstract class:
public class RoscosmosShuttle : Shuttle
{
}
- 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.
- 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.
- 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();
}
}
- 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)
{
}
推薦閱讀
- Learning Single:page Web Application Development
- Mastering JavaScript Functional Programming
- GAE編程指南
- The Android Game Developer's Handbook
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Learning ArcGIS Pro 2
- Learning Data Mining with Python
- Python進階編程:編寫更高效、優雅的Python代碼
- 焊接機器人系統操作、編程與維護
- 劍指大數據:企業級數據倉庫項目實戰(在線教育版)
- Python Interviews
- 一步一步跟我學Scratch3.0案例
- Mastering jQuery Mobile
- Instant Automapper
- Elasticsearch搜索引擎構建入門與實戰