Java 8(预发布)接口成员变量 [英] Java 8 (pre-release) interface member variables

查看:1013
本文介绍了Java 8(预发布)接口成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8中的公共成员变量是接口功能还是实现副作用/缺陷?

Are public members variables in Java 8 interfaces a feature or an implementation side-effect/defect?

这个问题与预发布的Java 8构建有关 lambda-8-b50-linux-x64-26_jul_2012.tar.gz

This question pertains to the pre-release Java 8 build lambda-8-b50-linux-x64-26_jul_2012.tar.gz.

Java 8以默认方法的形式向接口引入了新功能。使用JDK8 lambda编译器进行临时测试允许使用以下形式的接口:

Java 8 introduces new features to interfaces in the form of default methods. Casual testing with the JDK8 lambda compiler allows interfaces of this form:

public interface Foo {
  public int foo = 0;
  int foo() default { return foo; }
}

示例实现类型:

public class FooImpl implements Foo {
  public int foo = 1;
}

此代码遵循变量阴影的标准约定:

This code follows the standard conventions for variable shadowing:

Foo f = new FooImpl();
System.out.println(f.foo());
System.out.println(f.foo);
System.out.println(new FooImpl().foo);

输出:

0
0
1

文档(JSR 335 :用于Java™编程语言版本0.5.1的Lambda表达式)未提及成员变量。我倾向于认为编译器过于宽容,但也许我错过了一些东西。

The documentation (JSR 335: Lambda Expressions for the Java™ Programming Language Version 0.5.1) doesn't mention member variables. I'm inclined to think the compiler is being too tolerant but perhaps I've missed something.

推荐答案

接口中的公共字段是不是Java 8中的新功能。如果你记得它们是隐式静态和最终的,你看到的结果是完全合理的。

Public fields in interfaces are not a new features in Java 8. If you remember that they are implicitly static and final, the results you are seeing make perfect sense.

这篇关于Java 8(预发布)接口成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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