接口定义构造函数签名? [英] Interface defining a constructor signature?
问题描述
这很奇怪,这是我第一次碰到这个问题,但是:
It's weird that this is the first time I've bumped into this problem, but:
你如何在C#中的接口定义构造函数?
How do you define a constructor in a C# interface?
修改结果
有些人希望有一个例子(这是一个自由时间的项目,所以是的,这是一场游戏)
Edit
Some people wanted an example (it's a free time project, so yes, it's a game)
IDrawable结果
+更新结果
+绘制
IDrawable
+Update
+Draw
要能够更新(检查屏幕等边),并绘制它本身总是需要一个 GraphicsDeviceManager
。所以我想确保对象有对它的引用。这属于在构造函数中。
To be able to Update (check for edge of screen etc) and draw itself it will always need a GraphicsDeviceManager
. So I want to make sure the object has a reference to it. This would belong in the constructor.
现在,我写下来我觉得我实施这里是的IObservable
和 GraphicsDeviceManager
应采取在 IDrawable
...
这似乎不是我不明白的XNA框架或框架不是想出来的非常好。
Now that I wrote this down I think what I'm implementing here is IObservable
and the GraphicsDeviceManager
should take the IDrawable
...
It seems either I don't get the XNA framework, or the framework is not thought out very well.
修改结果
似乎有大约在接口方面,我国的构造函数的定义有些混乱。接口可以确实无法实例因此不需要构造。我想确定是构造一个签名。酷似一个接口可以定义一个特定方法的签名,该接口可以定义构造函数的签名。
Edit
There seems to be some confusion about my definition of constructor in the context of an interface. An interface can indeed not be instantiated so doesn't need a constructor. What I wanted to define was a signature to a constructor. Exactly like an interface can define a signature of a certain method, the interface could define the signature of a constructor.
推荐答案
您不能。这是偶尔的痛苦,但你不能用正常的方法反正调用它。
You can't. It's occasionally a pain, but you wouldn't be able to call it using normal techniques anyway.
在一篇博客文章中,我建议<一href=\"http://$c$cblog.jonskeet.uk/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces/\">static接口这将只可用在泛型类型的限制 - 但可能是非常方便的,IMO
In a blog post I've suggested static interfaces which would only be usable in generic type constraints - but could be really handy, IMO.
有关,如果你的可能的定义一个接口中的一个构造函数,你会遇到麻烦派生类的一点:
One point about if you could define a constructor within an interface, you'd have trouble deriving classes:
public class Foo : IParameterlessConstructor
{
public Foo() // As per the interface
{
}
}
public class Bar : Foo
{
// Yikes! We now don't have a parameterless constructor...
public Bar(int x)
{
}
}
这篇关于接口定义构造函数签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!