昨天的3个程序仅仅考察了final data一个类型
我们知道一共有3种方式来用final这个keyword.
final class是可以理解的,不可继承的.也没有什么特别可以说的地方,但是final method还是需要好好思考一下的,我们来看下原文怎么说的吧
The second reason for final methods is efficiency.If you make a method final , you ' re allowing the compiler to turn any calls to that method into inline calls.
学过C的都应该知道inline是怎么做的,为了避免有些朋友没有学过,我这里多嘴点(学过的可以跳到下面).inline函数也是一个函数,不过编译器在编译的时候只是将对这个函数的调用替换成函数体的内容而已,比如
inline int getJudge( int i); main() ... { for ( int index = 0 ;index < 100 ;index ++ ) printf( " %d " , & getJudege(index)); } int getJudege( int i) ... { return i; } // 编译完以后其实是这样的,只是代码替换避免了频繁调用 main() ... { for ( int index = 0 ;index < 100 ;index ++ ) printf( " %d " , & index); }
但是真实如书上所说呢,尽信书不如无书...最好还是自己来敲下代码才是最真实的.不过还是用昨天的类,只是做一些简单的修改
public class Test03 ... { public static void main(String[] args) ... { System.out.println( " Test01 main invoke: num = " + Test04.getI()); Test03 a = null ; } }class Test04 ... { static int i = 12 ; static ... { System.out.println( " Test02 static block: num = " + Test04.getI()); } final static int getI() ... { return i; } }
结果呢,我很兴奋的等着结果...可结果让我一点都没劲
Test02 static block: num = 12 Test01 main invoke: num = 12
我靠...那你JJYY说这么多有什么用呢? 那书上讲的是错的拉?没有...他欺骗了我们的感情..不只是你的,还有我的.....最后他来了一句,但是编译器有权决定是否要把final的设置为inline的,你只是允许他设置,不代表他真的会设置.
学学yami..7*&*^%&*^&%&^^*$&^&*&^%*&^&*^.一阵青烟过后,大家看见书的作者满脸大包走了向大家道歉....没错,就是我打的.....呵呵,很黄很暴力...