试图编程到C#中的抽象,但界面和类都没有真正起作用
我一直在尝试在我当前的项目上更加有意识地应用扎实的原则。使用接口来创建抽象并允许处理依赖注入以提供具体的类,这确实有助于解耦,从长远来看,(希望!)使其更加可维护。
但是,在这里和那里,我撞到了一堵墙,似乎界面和抽象类都没有起作用,原因是我想要定义实现的功能。
这意味着:
- 接口将无法工作,因为我无法定义实现,并且显然不想重复所有实现类中的代码,
- 因为我不能从多个类中派生
一些超级简单的代码来说明问题:
public abstract class Vehicle
{
public void MoveForward()
{
// Some code here
// This implementation is always the same
}
public abstract void PerformUniqueAbility(); // This is for the derived class to implement
}
public abstract class RadioSignalBroadcaster
{
public void StartBroadcast()
{
// Some code here
// This implementation is always the same
}
public abstract void PerformUniqueBroadcastingAbility(); // This is for the derived class to implement
}
当然,我想做的是:
public class MyNewClass: Vehicle, RadioSignalBroadcaster
{
// Class that contains the implementations for both MoveForward() AND StartBroadcast() but also allows me to define
// bodys for the abstract methods
public override void PerformUniqueAbility()
{
// class specific code here
}
public override void PerformUniqueBroadcastingAbility()
{
// class specific code here
}
}
我不能这样做,因为错误:
错误CS1721类“ myNewClass”不能有多个基类:“车辆”和“ RadioSignalBroadcaster”
当然, 场景?
I've been trying to apply SOLID principles more consciously on my current project. Using interfaces to create the abstraction and allowing classes that are handling the dependency injection to provide the concretions has really helped with decoupling some of the code and (hopefully!) making it more maintainable in the long run.
However, here and there I'm hitting a bit of a wall where it seems neither interfaces nor abstract classes work for the reason that there are functions for which I want an implementation defined.
This means:
- Interfaces will not work since I can't define an implementation and obviously don't want to repeat the code in all implementing classes
- Abstract classes will not work because I cannot derive from multiple classes
Some super simple code to illustrate the problem:
public abstract class Vehicle
{
public void MoveForward()
{
// Some code here
// This implementation is always the same
}
public abstract void PerformUniqueAbility(); // This is for the derived class to implement
}
public abstract class RadioSignalBroadcaster
{
public void StartBroadcast()
{
// Some code here
// This implementation is always the same
}
public abstract void PerformUniqueBroadcastingAbility(); // This is for the derived class to implement
}
Now of course what I'd like to do is this:
public class MyNewClass: Vehicle, RadioSignalBroadcaster
{
// Class that contains the implementations for both MoveForward() AND StartBroadcast() but also allows me to define
// bodys for the abstract methods
public override void PerformUniqueAbility()
{
// class specific code here
}
public override void PerformUniqueBroadcastingAbility()
{
// class specific code here
}
}
Of course I cannot do this because of the error:
Error CS1721 Class 'MyNewClass' cannot have multiple base classes: 'Vehicle' and 'RadioSignalBroadcaster'
What's the best way to approach these scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用具有默认实现的接口,这些接口在C#8中引入。然后,您可以从这些接口中得出。
以下是如何提供
MoveForward()
和startBroadcast()
方法的默认实现的示例:You could use interfaces with default implementations which were introduced in C# 8. Then you could derive from these interfaces.
Here's an example of how you could you provide default implementations for the
MoveForward()
andStartBroadcast()
methods:您不能继承超过1个类,但是可以继承多个接口。这是您想要的吗?
You can't inherit more than 1 class but you can inherit more than one interface. Is this what you are looking for?
C#类只能从一个基类继承,但可以从任何数量的接口继承。
如果您的目标是将多个基本类继承为
myNewClass
,则可以将您的一个抽象类更改为从另一个中继承的:但是,正如您可以从此方法中看到的那样,它违反了单个责任原则现在为
radioSignalBroadcast
(现在myNewClass
)有多个更改的理由(如果更改为perhat>车辆>车辆
或> radioSignalBroadcast
逻辑)。任何基本类别发生的任何更改都会传播到从这些基类继承的所有其他类中,这可能是您所追求的也可能不是。这完全取决于您的应用程序的设计。要问自己的问题:
车辆
和radioSignAlBroadcast
是抽象类,还是很容易成为接口?从实现的外观上看,您有几种方法要分享到派生的课程中,因此我了解您想将它们保留为基础课程,但要记住这一点。另外,请查看moveforward
和startBroadcast
是否可以具有默认接口实现。myNewClass
是否需要实现两个基类/接口?两个单独的课程不能更好吗?分开这样的课程有助于将每个班级集中到一个单一的责任。myNewClass
不是真正的车辆
或radioSignalBroadcast
(按照上一点)例如,这两个:让我知道您是否要指出示例或更多要点。
C# classes can only inherit from one base class, but can inherit from any number of interfaces.
If your goal is to have multiple base classes being inherited to
MyNewClass
, you could change one of your abstract classes to inherit from the other, for example:However, as you can see from this approach, it violates Single Responsibility Principle as now
RadioSignalBroadcast
(and nowMyNewClass
) has more than one reason to change (if there's a change toVehicle
orRadioSignalBroadcast
logic). Any change that happens to any of the base classes will propagate to all other classes which inherit from those base classes, which may or may not be what you're after.That entirely depends on the design of your application. Questions to ask yourself:
Vehicle
andRadioSignalBroadcast
to be abstract classes, or can it easily be an interface? By the looks of your implementation, you have a couple of methods which you want to share to your derived classes so I understand you wanting to keep them as base classes, but it's something to keep in mind. Also check out if the implementation ofMoveForward
andStartBroadcast
can have a default interface implementation.MyNewClass
need to implement both base classes/interfaces? Couldn't two separate classes work out better? Separating out classes like this helps to focus each of the classes to have one single responsibility.MyNewClass
is not truly aVehicle
or aRadioSignalBroadcast
(as per the previous point), can this object be composed by a combination of either of the two, for example:Let me know if you want example or more points to point out.
我认为乔纳斯(Jonas)为您提供了最好的答案,您可以使用默认的
接口
实现。但是,我保留帖子,因为它提供了信息,如何实现相同的效果,使用没有此语言功能的技术。您将创建其他类似的类:
I think Jonas gave you the best answer that you can use default
interface
implementations. However I keep my post, because it gives information, how to achieve same effect, using technology without this language feature.You will create another classes like that: