`
suifongqi
  • 浏览: 54236 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JFX学习笔记 七 增量式求值和懒惰求值

    博客分类:
  • JAVA
阅读更多
/*
* Main.fx
*
* Created on 2008-11-24, 9:04:46
*/

package javafxapplication3;
/**
* @author notpeeging
*/
// place your code here
import java.lang.System;

class X {
attribute a: Number;
attribute b: Number;
attribute c: Number;
}
//
// trigger on X.b = newValue {
// System.out.println("X.b is now {newValue}");
// }
//
// trigger on X.c = newValue {
// System.out.println("X.c is now {newValue}");
// }

var x1 = X {
a: 1
b: 2   // X.b is now 2 is printed
c: 3   // X.c is now 3 is printed
};

var x2 = X {
a:  x1.a       // eager, non-incremental
b:  bind x1.b // eager, incremental (X.b is now 2 is printed)
// c:  bind lazy x1.c  // lazy, incremental (nothing is printed yet)
};

System.out.println(x2.a); // prints 1
System.out.println(x2.b); // prints 2
System.out.println(x2.c); // prints X.c is now 3, then prints 3

x1.a = 5;
x1.b = 5; // prints X.b is now 5, twice
x1.c = 5; // prints X.c is now 5, twice

System.out.println(x2.a); // prints 1
System.out.println(x2.b); // prints 5
System.out.println(x2.c); // prints 5
注释的地方本可以通过,但不知道为什么会报错。。。无语

例中,x2的属性b和c被绑定到x1的属性b和c。这意味着当x1的b或c属性被更新时,x2的b或c属性都会相应地被更新。在x2中的b、c属性之间的不同是:前者的属性值在其属性初始化程序中被立即更新,而后者的绑定直到其值被访问时才被求值。

注意:函数体无需bind操作符便可被增量地求值,但操作体则做不到。在改变本地变量的操作中并不触发增量式求值。增量式求值不能在操作体内执行,除了表达式明确地以bind作为前缀。

然而,当你在一个增量式求值上下文中调用操作或者Java方法,此调用本身将被增量式求值。这意味着如果此调用变成了对操作或者Java方法的全新调用(由于此调用被增量式求值),那么它所用到的任何参数值将被使用并返回新值。

译者注:此处的新值是与不进行增量式求值相比。

相反,在增量式求值上下文中调用函数,此函数只能被调用一次,而其求值结果也将被合并到调用求值树中。

增量式求值是JavaFX的主要特征,它使定义复杂的动态GUI声明成为了可能。懒惰求值的特性常用在处理像tree或者graph这样的递归数据结构上。

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics