![](/img/trans.png)
[英]method to take string inside curly braces using split or tokenizer
[英]Using curly braces inside object initialization block
為什么僅在范圍大括號內設置函數bind()
存在?
public void initialize() {
inputsAreFull = new BooleanBinding() {
{
bind();
}
@Override
protected boolean computeValue() {
return false;
}
};
}
IntelliJ在大括號內時會自動推薦bind()
,但在大括號內不存在該功能嗎?
這行不通:
public void initialize() {
inputsAreFull = new BooleanBinding() {
bind();
@Override
protected boolean computeValue() {
return false;
}
};
}
您使用的語法是用於聲明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;
}
};
}
沒有初始化程序塊,您不能在類級別隨機調用方法。 您可以通過編寫來測試...
class MyClass extends BooleanBinding {
bind(); // It's not gonna be very happy with you.
@Override
protected boolean computeValue() {
return false;
}
}
具有運行示例的IDEOne: http ://ideone.com/EERkXB
另請參見什么是初始化塊?
new BooleanBinding() { ... }
引入了BooleanBinding
的匿名子類。
現在bind
是一個受保護的方法,因此不允許執行inputsAreFull.bind()
。
但是可以在子類主體中的匿名初始化程序塊{ ... }
中調用bind。
還有一點需要注意:由於此時對象尚未完全初始化; 如果實際上是在BooleanBinding構造函數中執行的代碼(由編譯器負責),則方法bind
應該不可重寫。 為此,可以使用private
或(這里) protected final
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.