小编mja*_*lil的帖子

为什么Java允许接口具有静态只读字段而.NET接口不能?

我面对Java中的示例代码,它给我带来了一个问题.

Java示例代码是:

...
public interface CLibrary extends Library {
    CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
    void printf(String format, Object... args);
}

public static void main(String[] args) throws IOException {
    CLibrary.INSTANCE.printf("Hello, World\n");
}
Run Code Online (Sandbox Code Playgroud)

但是在C#中我们不能这样写:

public interface IMyInterface {
    static readonly int staticInt = 5;                          // compile error
    static readonly SomeClass staticInstance = new SomeClass(); // compile error
}
Run Code Online (Sandbox Code Playgroud)

这两种语言/框架有什么区别?

什么设计策略允许java在接口中使用const字段或什么阻止.NET拥有它?

.net java language-design

5
推荐指数
1
解决办法
1381
查看次数

标签 统计

.net ×1

java ×1

language-design ×1