为什么不能指定接口的静态方法? [英] Why can't interfaces specify static methods?
问题描述
我知道这个问题已经被问了个遍,但我似乎无法找到足够好的答案。因此要清楚什么,我想知道,我将在两个问题分开这样的:
-
为什么 T接口具有静态方法的签名?我会尽力抢占非回答询问为什么在世界上我想用下面这样做:我会希望能够调用静态
GetDbConnectionType()
在SqliteCodeGenerator
和MssqlCodeGenerator
:接口ICodeGenerator
{
//这是我想是静态的方法:
串GetDbConnectionType();
}
抽象类CodeGeneratorBase:ICodeGenerator
{
公共抽象的字符串GetDbConnectionType();
公共无效GenerateSomeCode(StringBuilder的S)
{
s.AppendLine(无功富=新的+ GetDbConnectionType()+(););
}
}
类SqliteCodeGenerator:CodeGeneratorBase
{
公众覆盖字符串GetDbConnectionType()
{
返回SQLiteConnection ;
}
}
类MssqlCodeGenerator:CodeGeneratorBase
{
公众覆盖字符串GetDbConnectionType()
{
回报的SqlConnection ;
}
}
-
在另一方面,这是这第二个问题的事情,如果你知道一个很好的选择,以通过各种手段...
达到上述目标,那么
假设你的可能的指定一个接口,一个类型必须有一个特定的静态方法......你会怎么称呼呢?多态性致力于通过的实例的 - 而静态成员明确的不的使用情况
现在,话虽如此,还有的有一个的情况是,我可以看到静态接口成员的工作:泛型类型。例如:
//这是无效的代码...
公共无效美孚< T>()其中T:ICodeGenerator
{
字符串类型= T.GetDbConnectionType();
}
这将呼吁的的静态成员的具体的键入 T
。
我已经的博客更多有关此,但我怀疑的利益并不能证明的复杂性。
在替代品方面 - 通常你必须另一个接口,并有不同的类型,以实现该接口。行之有效在某些情况下,而不是在其他
I know this question has been asked over and over, but I can't seem to find good enough answers. So to make it clear what I'm trying to know, I'll split this in two questions:
Why can't interfaces have static method signatures? I'll try to preempt the non-answers asking why in the world I would want to do this with the following: I would want to be able to statically invoke
GetDbConnectionType()
onSqliteCodeGenerator
andMssqlCodeGenerator
:interface ICodeGenerator { // this is the method I would like to be static: string GetDbConnectionType(); } abstract class CodeGeneratorBase : ICodeGenerator { public abstract string GetDbConnectionType(); public void GenerateSomeCode(StringBuilder s) { s.AppendLine("var foo = new " + GetDbConnectionType() + "();"); } } class SqliteCodeGenerator : CodeGeneratorBase { public override string GetDbConnectionType() { return "SQLiteConnection"; } } class MssqlCodeGenerator : CodeGeneratorBase { public override string GetDbConnectionType() { return "SqlConnection"; } }
On the other hand, and this is the matter of this second question, if you know of a good alternative to reach the aforementioned goal, then by all means...
Suppose you could specify in an interface that a type had to have a particular static method... how would you call it? Polymorphism works through instances - whereas static members explicitly don't use instances.
Now, having said that, there's one situation in which I can see static interface members working: generic types. For example:
// This isn't valid code...
public void Foo<T>() where T : ICodeGenerator
{
string type = T.GetDbConnectionType();
}
That would call the static member on the concrete type T
.
I've blogged more about this, but I suspect the benefit doesn't justify the complexity.
In terms of alternatives - usually you'd have another interface, and have separate types to implement that interface. That works well in some contexts, but not in others.
这篇关于为什么不能指定接口的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!