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



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

我该怎么办?

我有以下接口:

public interface ICar
{
void Move();
void GetService();
}
public interface ISuperCar : ICar
{
void MoveFast();
void HaveARace();
}

和抽象类:

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();
}

及其实现:

public class ToyotaCar : Car
{
public override void GetService()
{
Console.WriteLine("Getting ToyotaService");
}
}
public class ToyotaSuperCar : SuperCar
{
public override void GetService()
{
Console.WriteLine("Getting ToyotaService");
}
public override void HaveARace()
{
Console.WriteLine("Have a race in Toyota competition");
}
}

问题是如何不使方法GetService((加倍。从逻辑上讲,它应该通过:

public class ToyotaSuperCar : SuperCar, ToyotaCar
{
public override void HaveARace()
{
Console.WriteLine("Have a race in Toyota competition");
}
}

但是多重继承在C#中不可用

您可以使用组合而不是继承。使SuperCar类实现ICar,而不是从Car继承,并在将方法委托给的构造函数中传递一个ICar对象:

public abstract class SuperCar : ICar, ISuperCar
{
private readonly ICar car;
public SuperCar(ICar car)
{
this.car = car;
}
public void Move() => car.Move();
public void GetService() => car.GetService();
public void MoveFast()
{
Console.WriteLine("SuperCar moves fastly");
}
public abstract void HaveARace();
}

你的ToyotaSuperCar类看起来是这样的:

public class ToyotaSuperCar : SuperCar
{
public ToyotaSuperCar(ToyotaCar car) : base(car) { }
public override void HaveARace()
{
Console.WriteLine("Have a race in Toyota competition");
}
}

您需要这样做:

public class SuperCarPart : Car, ISuperCar
{
public override void GetService() { throw new NotImplementedException(); }
public void HaveARace() { throw new NotImplementedException(); }
public void MoveFast() { throw new NotImplementedException(); }
}
public class ToyotaSuperCar : ToyotaCar, ISuperCar
{
private SuperCarPart _superCarPart = new SuperCarPart();
public void HaveARace()
{
_superCarPart.HaveARace();
}
public void MoveFast()
{
_superCarPart.MoveFast();
}
}

一种可能性是将服务声明为其自己的接口

public interface ICarService
{
void CarryOutService();
}

汽车接口看起来是这样的:

public interface ICar
{
ICarService Service { get; }
void Move();
}
public interface ISuperCar : ICar
{
void MoveFast();
void HaveARace();
}

抽象类需要一个具有ICarService参数的构造函数。

public abstract class Car : ICar
{
public Car(ICarService service) => Service = service;
public ICarService Service { get; }
public void Move()
{
Console.WriteLine("Car moves");
}
}
public abstract class SuperCar : Car, ISuperCar
{
public SuperCar(ICarService service)
: base(service)
{
}
public void MoveFast()
{
Console.WriteLine("SuperCar moves fast");
}
public abstract void HaveARace();
}

一种可能的服务实现:

public class ToyotaService : ICarService
{
public void CarryOutService()
{
Console.WriteLine("Carrying out ToyotaService");
}
}

在car的具体实现中,您可以选择在构造函数中传递服务,也可以直接实例化它们。由于丰田汽车总是有一个ToyotaService,您可以直接实例化它们。为了获得更大的灵活性,请使用构造函数方法。

public class ToyotaCar : Car
{
public ToyotaCar()
: base(new ToyotaService())
{
}
}
public class ToyotaSuperCar : SuperCar
{
public ToyotaSuperCar()
: base(new ToyotaService())
{
}
public override void HaveARace()
{
Console.WriteLine("Have a race in Toyota competition");
}
}

用法:

var car = new ToyotaSuperCar();
car.Service.CarryOutService();

我们在这里遵循了组合而非继承的原则。它还有一个额外的优点,那就是将来更容易扩展服务接口,因为这不会影响汽车的实现。

相关内容

  • 没有找到相关文章

最新更新