在 C# 中强制接口实现实现层次结构
我正在为新项目编写界面,想获得一些建议。
我有一个类,它有一个子类,它也有一个子类。 这个类的树是这样的:
Class Car
{
Wheels Wheel;
}
Class Wheels
{
Rims Rim;
}
简单来说:一辆车有一个轮子,一个轮子有一个轮辋。 (抱歉,无法举出其他更好的例子)。
因此,我想在 ICar、IWheels 和 IRims 的接口实现中强制采用这种层次结构。
所以我做了这样的事情(在 C# 中):
ICar
{
IWheels Wheel;
}
IWheels
{
IRims Rim;
}
我有一个错误,接口实现中不能有字段。 所以这让我开始意识到这可能是错误的界面设计。 我想强制接口实现来实现这种层次结构。 但也许根据设计模式和最佳实践,应该以其他方式完成?
您能否告诉我如何设计我的系统,以便强制对象实现这种层次结构?
也许我的问题有些不准确,或者我遗漏了一些重要信息。 如果是,请在评论中询问。
I m writing interfaces for new project and would like to get some advice.
I have a class that have a subclass and it has a subclass. The tree of this classes is like this:
Class Car
{
Wheels Wheel;
}
Class Wheels
{
Rims Rim;
}
So to simplify: one car has one wheel and one wheel has one rim. (cant make up other better example, sorry).
So I would like to force this hierarchy in my interface implementation of ICar, IWheels and IRims.
So i did something like this (in C#):
ICar
{
IWheels Wheel;
}
IWheels
{
IRims Rim;
}
And i have a error that I can not have fields in interface implementation. So this started me thing that maybe it's wrong interface design. I would like to force interface implementations to implement this kind of hierarchy. But maybe accorting to design patterns and best practices it should be done in other way?
Could you please tell me how to design my system so that objects will be forced to implement this kind of hierarchy?
Maybe there is something not precise in my question or I missing some important info. If yes, please ask in comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的接口中,您必须明确 Wheels 应该是 ICar 的属性,因为您无法声明接口实现应该具有哪些字段。 (字段是内部运作的,因此接口不应该知道它)。
In your interface, you'll have to make it clear that Wheels should be a property of ICar, since you cannot declare which fields an interface implementation should have. (Fields are inner workings, so the interface should not know about it).
您不能在接口中指定字段(而且您不应该这样做 - 这是实现决策),但您可以指定属性:
您可能想要只将 getter 放在接口中 - 在接口中包含 setter 有点不寻常:(
如果您想覆盖现有(或抽象)属性,该属性只有 getter 来添加 setter,那么会有些奇怪,但没关系我相信,也可以用 setter 实现“仅 getter”接口。)
You can't specify a field in an interface (and you shouldn't be able to - that's an implementation decision) but you can specify a property:
You may well want to only put the getter in the interface though - it's slightly unusual to include a setter in an interface:
(There are oddities if you want to override an existing (or abstract) property which only has a getter to add a setter, but it's okay to implement a "getter-only" interface with setters as well, I believe.)
您不能声明字段,但可以声明属性。 这将产生与强制特定类提供另一个类的实例相同的最终效果。
You can't declare fields, but you can declare properties. That will have the same end effect of forcing a particular class to provide an instance of another class.
正如错误所示,您无法在界面中指定字段。 您可以指定属性:
As the error says, you can't specify fields in your interfaces. You can specify properties though:
我不太习惯 C#,但在我看来,您可以通过创建抽象类以及您想要使用的字段来强制实现该实现。
因此,如果您扩展这些抽象类,您将可以使用其中的字段。
不过,你必须创建一个抽象类和一个接口......
I'm not so much used to C# but it sounds to me that you can force that implementation by making Abstract classes, with the fields you want to use.
So if you extend those abstract classes you will have the fields in them available.
You'll have to make an abstract class AND an interface though...
这是一个功能齐全的代码......
希望能帮助到你...
Here it is a fully functional code...
Hope it helps...