C# 为什么我们不允许在接口中指定构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are we not allowed to specify a constructor in an interface?
提问by Pondidum
Possible Duplicate:
Interface defining a constructor signature?
可能的重复:
定义构造函数签名的接口?
I know that you cannot specify a constructor in an interface in .Net, but why can we not?
我知道你不能在 .Net 的接口中指定构造函数,但为什么不能呢?
It would be really useful for my current project to be able to specify that an 'engine' must be passed in with the constructor, but as I cant, I have to suffice with an XML comment on the class.
对于我当前的项目来说,能够指定必须通过构造函数传入一个“引擎”真的很有用,但是我不能,我必须对类进行 XML 注释就足够了。
采纳答案by cletus
Because an interface describes behaviour. Constructors aren't behaviour. How an object is built is an implementation detail.
因为接口描述了行为。构造函数不是行为。如何构建对象是一个实现细节。
回答by Mork0075
Because you cant instantiate an interface, so a constructur doenst make sense.
因为你不能实例化一个接口,所以构造器没有意义。
回答by Jon Skeet
How would you call the constructor? When you use interfaces, you normally pass an instance of the interface around (or rather, a reference). Also bear in mind that if one class implements an interface, a derived class inherits that interface, but may not have the same set of constructors.
你会如何调用构造函数?当您使用接口时,您通常会传递接口的一个实例(或者更确切地说,是一个引用)。还要记住,如果一个类实现了一个接口,派生类将继承该接口,但可能没有相同的构造函数集。
Now, I can see the use of what I call static interfacesfor specifying constructors and other essentially static members for use in generic methods. See my blog post on the ideafor more information.
现在,我可以看到使用我所说的静态接口来指定构造函数和其他本质上是静态的成员以用于泛型方法。有关更多信息,请参阅我关于该想法的博客文章。
回答by Tony
No you can not have constructors on interfaces for the reasons that have been posted. However you can on abstract classes. Lets say for example you have this base class.
不,由于已发布的原因,您不能在接口上使用构造函数。但是,您可以在抽象类上。例如,让我们说你有这个基类。
public abstract class ClassOne
{
protected int _x;
protected string _s;
public ClassOne(int x, string s)
{
_x = x;
_s = s;
}
}
Notice there is no constructors that takes no argument (default constructor) which means any class that inherits from ClassOne must call the constructor that has 2 arguments.
请注意,没有不带参数的构造函数(默认构造函数),这意味着从 ClassOne 继承的任何类都必须调用具有 2 个参数的构造函数。
So this is not valid and will not compile.
所以这是无效的,不会编译。
public class ClassTwo : ClassOne
{
public ClassTwo()
{ }
}
However this is valid and will compile.
但是,这是有效的并且可以编译。
public class ClassTwo : ClassOne
{
public ClassTwo(int x, string s) : base(x, s)
{ }
}
I would like to point out here that in C# you can only inherit from one base class. Meaning that this may not be the correct solution for particular situation but is something to think about.
我想在这里指出,在 C# 中,您只能从一个基类继承。这意味着这可能不是特定情况的正确解决方案,但值得考虑。
Tony.
托尼。
回答by M.Turrini
Among all the other reasons already posted, keep also in mind the a class can easily implement several interfaces; which constructor should be used then?
在已经发布的所有其他原因中,还要记住一个类可以轻松实现多个接口;那么应该使用哪个构造函数?
回答by Wim Coenen
Other answers have already pointed out why it doesn't make sense to have a constructor declaration on an interface. But from your question, I'm guessing that you are probably looking for the abstract factory pattern.
其他答案已经指出为什么在接口上声明构造函数没有意义。但是从您的问题来看,我猜您可能正在寻找抽象工厂模式。
To give an example based on your question: you say that you would like to somehow declare that an 'engine' must be passed to the constructor. You can do this by declaring a separate interface for a construction service like this:
根据您的问题举一个例子:您说您想以某种方式声明必须将“引擎”传递给构造函数。你可以通过为这样的构造服务声明一个单独的接口来做到这一点:
public interface IGadgetFactory
{
IGadget CreateGadget(Engine engine);
}
Any code which must create IGadget
instances can then use an IGadgetFactory
instance instead of calling any constructors directly.
任何必须创建IGadget
实例的代码都可以使用IGadgetFactory
实例而不是直接调用任何构造函数。
回答by Damien_The_Unbeliever
Besides the other explanations given here, you'd have to invent a new syntax for calling them anyway, since if you have two or more implementations in scope at the line:
除了此处给出的其他解释之外,您无论如何都必须发明一种新的语法来调用它们,因为如果您在该行的范围内有两个或多个实现:
Dim x as new IDoStuff()
Whice implementation gets called?
调用哪个实现?