Created
September 2, 2012 12:03
-
-
Save Liutos/3597752 to your computer and use it in GitHub Desktop.
举个有惰性可以方便的例子
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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