静力学与接口 [英] Statics & Interfaces

查看:73
本文介绍了静力学与接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面,我希望有一些静态函数。

当我添加静态时对于任何界面函数,我得到

以下错误:修饰符''静态''对此项目无效。

为什么不可能在界面中定义静态函数?


谢谢

I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier ''static'' is not valid for this item".
Why is it not possible to define static functions in an interface?

Thanks

推荐答案

< hu * ****** @ yahoo.com在留言中写道

新闻:11 ********************* @ b28g2000cwb.googlegro ups。 com ...
<hu*******@yahoo.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

>我有一个接口,我想要一些静态函数。

当我添加静态时;对于任何界面函数,我得到

以下错误:修饰符''静态''对此项目无效。

为什么不可能在接口中定义静态函数?
>I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier ''static'' is not valid for this item".
Why is it not possible to define static functions in an interface?



也许你应该做一个抽象课?

Maybe you should make an abstract class instead?


>

谢谢
>
Thanks



< hu ******* @ yahoo.comwrote:
<hu*******@yahoo.comwrote:

我有一个接口,我希望有一些静态功能。

当我添加静态时对于任何界面函数,我得到

以下错误:修饰符''静态''对此项目无效。

为什么不可能在接口中定义静态函数?
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier ''static'' is not valid for this item".
Why is it not possible to define static functions in an interface?



CLR允许在接口中实现静态方法,但即使是
那么你也不能只指定一个静态方法由实现接口的类型实现的
。 C#甚至不会让静态方法首先放在那里。


我同意它有时很有用,但请考虑:你怎么会首先打电话给

静态方法?


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

The CLR allows static methods to be implemented in interfaces, but even
then you couldn''t just specify a static method which has to be
implemented by the type implementing the interface. C# doesn''t even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too





首先我们应该理解关键字static; static表示

它将拥有自己的内存区域而不管它的实例

意味着这个函数将被加载到内存中而没有它的

实例。


现在想想我们是否有一个接口并且在没有正文的情况下将一个静态函数声明为

。现在将加载到内存中,如果用户

将调用IInterface.StaticFunction(),将执行什么。所以我想b $ b认为它在界面中有一个静态函数是完全没用的。

-Rohit


Jon写道:
Hi

First of all we should understand the keyword static; static says that
it would be having its own memory area irrespective of its instance
means this function will be loaded into the memory without having its
instance.

Now think if we have an interface and have one static function declared
in it without body. Now what will be loaded into the memory and if user
will call IInterface.StaticFunction(), what will be executed. So I
think its totally useless having a static function in interface.
-Rohit

Jon wrote:

< hu ******* @ yahoo.comwrote:
<hu*******@yahoo.comwrote:

我有一个界面,我会喜欢有一些静态函数。

当我添加静态时对于任何界面函数,我得到

以下错误:修饰符''静态''对此项目无效。

为什么不可能在接口中定义静态函数?
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier ''static'' is not valid for this item".
Why is it not possible to define static functions in an interface?



CLR允许在接口中实现静态方法,但即使是
那么你也不能只指定一个静态方法由实现接口的类型实现的
。 C#甚至不会让静态方法首先放在那里。


我同意它有时很有用,但请考虑:你怎么会首先打电话给

静态方法?


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


The CLR allows static methods to be implemented in interfaces, but even
then you couldn''t just specify a static method which has to be
implemented by the type implementing the interface. C# doesn''t even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于静力学与接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆