小编Dan*_*el 的帖子

如何在 C# 中组织抽象类层次结构

我在 C# 中遇到了一种情况,当我需要多重继承以避免在实现类中重复代码时,但这在 C# 中是被禁止的。

我应该怎么办?

我有以下接口:

public interface ICar
{
   void Move();
   void GetService();
}

public interface ISuperCar : ICar
{
   void MoveFast();
   void HaveARace();
}
Run Code Online (Sandbox Code Playgroud)

和抽象类:

public abstract class Car : ICar
{
   public void Move()
   {
      Console.WriteLine("Car moves");
   }

   public abstract void GetService();
}

public abstract class SuperCar : Car, ISuperCar
{
   public void MoveFast()
   {
      Console.WriteLine("SuperCar moves fastly");
   }

   public abstract void HaveARace();
}
Run Code Online (Sandbox Code Playgroud)

以及他们的实现:

public class ToyotaCar : Car
{
   public override void GetService()
   { …
Run Code Online (Sandbox Code Playgroud)

c# inheritance abstract-class multiple-inheritance

1
推荐指数
1
解决办法
256
查看次数