- 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)
{
}
推薦閱讀
- DevOps with Kubernetes
- 算法零基礎一本通(Python版)
- 深入淺出Electron:原理、工程與實踐
- Photoshop智能手機APP UI設計之道
- Python數據可視化:基于Bokeh的可視化繪圖
- Java面向對象軟件開發
- Java開發入行真功夫
- 零基礎學Python數據分析(升級版)
- Gradle for Android
- RESTful Java Web Services(Second Edition)
- Hands-On Kubernetes on Windows
- ActionScript 3.0從入門到精通(視頻實戰版)
- Learning Concurrency in Python
- Offer來了:Java面試核心知識點精講(框架篇)
- 一覽眾山小:ASP.NET Web開發修行實錄