Skip to content

Instantly share code, notes, and snippets.

@Liutos
Created September 2, 2012 12:03
Show Gist options
  • Save Liutos/3597752 to your computer and use it in GitHub Desktop.
Save Liutos/3597752 to your computer and use it in GitHub Desktop.
举个有惰性可以方便的例子
private void func(int data) {
long time = getTime(); // 如果没有惰性,那么这里一定会被求值。
switch(data) {
case 1:
funcNeedTime1(data, time); // 需要使用time变量
break;
case 2:
funcNeedTime2(data, time); // 需要使用time变量
break;
case 3:
// 不需要使用time变量,所以time的计算是无用功。如果有惰性,那么如果分支到了这里,time也不会被计算
funcDoNotNeedTime(data);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment