为什么bind()仅在将范围大括号内设置时该函数存在?
public void initialize() {
inputsAreFull = new BooleanBinding() {
{
bind();
}
@Override
protected boolean computeValue() {
return false;
}
};
}
Run Code Online (Sandbox Code Playgroud)
IntelliJ会自动建议bind()在花括号内使用,但是在花括号外不存在该功能吗?
这行不通:
public void initialize() {
inputsAreFull = new BooleanBinding() {
bind();
@Override
protected boolean computeValue() {
return false;
}
};
}
Run Code Online (Sandbox Code Playgroud)
您使用的语法是用于声明type实现的快捷方式BooleanBinding。您实际上是在类声明中。
public void initialize(){
inputsAreFull = new BooleanBinding() {
// This is equivalent to a class level scope for your anonymous class implementation.
{
bind();
}
@Override
protected boolean computeValue() {
return false;
}
};
}
Run Code Online (Sandbox Code Playgroud)
没有初始化程序块,您不能在类级别随机调用方法。您可以通过编写来测试...
class MyClass extends BooleanBinding {
bind(); // It's not gonna be very happy with you.
@Override
protected boolean computeValue() {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
具有运行示例的IDEOne:http ://ideone.com/EERkXB
另请参见什么是初始化块?
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |