问题描述
在.NET中,我们不允许在抽象类和接口共享功能/方法。他们为什么不允许?
In .net we are not allowed to have shared function/methods in abstract classes and interfaces.Why they are not allowed ?
这是相同的其他语言。像Java?
Is this same in other languages. like Java ?
可能是潜在的问题如果共享的方法是允许的?
What can be the potential problem if the Shared methods are allowed ?
推荐答案
您一定可以在抽象类的静态(共享)方法。你不能让他们在接口方面,但是。
You can certainly have static (shared) methods in abstract classes. You can't have them in interfaces, however.
这听起来像你真的想要的虚拟的静态/共享的方法 - 而这些都是不具备的。静态方法不叫多态,并与的的方式最的.NET程序的工作原理,这不会使一个很大的意义。这将是有意义的是能够在使用他们作为类型参数约束指定静态方法的接口 - an想法我以前现在的博客上讲述。
It sounds like you really want virtual static/shared methods - and those aren't available. Static methods aren't called polymorphically, and with the way that most of .NET works, that wouldn't make a lot of sense. It would make sense to be able to specify static methods in interfaces when using them as type parameter constraints - an idea I've blogged about before now.
Delphi有一个元类,其中(据我所知)在类的元类的实例方法都像静态方法在类本身的概念 - 一元类可以从另一个派生,重写方法等我不是一个Delphi程序员,但在果壳第2章德尔福可能是有益的你,如果你想了解更多信息。
Delphi has the concept of a meta-class, where (as I understand it) instance methods in a class's meta-class are like static methods in the class itself - and one meta-class can derive from another, overriding the methods etc. I'm not a Delphi programmer, but chapter 2 of Delphi in a Nutshell might be useful to you if you want more information.
Java允许常量在接口中规定,但这是有支持的唯一一种静态成员。
Java allows constants to be specified in interfaces, but that's the only kind of static member supported there.
有趣的是,CLI本身的确实的允许静态方法的接口,但是这与机构的方法 - 不只是签名是由一个接口的实例成员提供
Interestingly, the CLI itself does allow static methods in an interface, but that's methods with bodies - not just the signature which is provided by instance members of an interface.
这篇关于为什么我们不能有共用一个接口/抽象类(静态)函数/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!