Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

.net - Why we can not have Shared(static) function/methods in an interface/abstract class?

In .net we are not allowed to have shared function/methods in abstract classes and interfaces. Why they are not allowed ?

Is this same in other languages. like Java ?

What can be the potential problem if the Shared methods are allowed ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can certainly have static (shared) methods in abstract classes. You can't have them in interfaces, however.

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 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 allows constants to be specified in interfaces, but that's the only kind of static member supported there.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...