靜態數據初始化 你 真的了解了嗎?構造方法的初始之后。。我又做錯了,太粗心了

            下面的程序輸出什么呢?  考慮下哦。。。。

          public class Test {
              
          public static final Test TEST = new Test();
              
          private final int belt;
              
          private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
              
              
          public Test(){
                  belt 
          = CURRENT_YEAR - 1930;
              }

              
          public int getBelt(){
                  
          return belt;
              }

              
          public static void main(String []args) throws Exception{
                  System.out.println(TEST.getBelt());
              }

          }

          可能你覺得應該是當前年- 1930, 例如:今年是2009,2009-1930= 79,運行結果真的是這樣嗎?
          你運行下,額,奇怪,居然是 -1930,  額,為什么呢??

            原來 首先其靜態域 被設置為缺省值, TEST先設置為null, belt設置為0 , 然后TEST構造器計算出來,但我們已經初始化belt了,
          belt被設置為final, 所以忽略了。。。 
          再來看下 下面一個簡單的例子,剛開始做的時候不仔細,哎,, 我錯了。。哎~!~ 希望大家不要跟我一樣啊。
           1 class Point {
           2     protected final int x, y;
           3     private final String name;
           4 
           5     Point(int x, int y) {
           6         this.x = x;
           7         this.y = y;
           8         name = makeName();
           9     }
          10 
          11     protected String makeName() {
          12         return "[" + x + "," + y + "]";
          13     }
          14     
          15     public final String toString(){
          16         return name;
          17     }
          18 
          19 }
          20 
          21 public class ColorPoint extends Point {
          22     private final String color;
          23     
          24     ColorPoint(int x, int y, String color){
          25         super(x,y);
          26         this.color = color;
          27     }
          28     protected String makeName() {
          29         return super.makeName()+":"+color;
          30     }
          31     
          32     public static void main(String[] args) {
          33         System.out.println(new ColorPoint(1,2,"abc"));
          34     }
          35     
          36 }

          運行結果:  [1,2]:null
          程序從main啟動,然后到 25行,super(x,y);   之后 到 第 8行   name = makeName();  再之后29行, return super.makeName()+":"+color;
          這里,方法被子類重載了,運行到26行  this.color = color;  最后結束, 當然輸出: [1,2]:null  

          posted on 2009-04-15 19:28 胡鵬 閱讀(234) 評論(1)  編輯  收藏 所屬分類: java基礎

          評論

          # re: 靜態數據初始化 你 真的了解了嗎? 2009-04-16 11:29 重慶理工小子

          final是常量,不是靜態數據。
          靜態是 static
          對于這個初始化,常量和變量的申明,可以看看我的這篇文章

          http://www.aygfsteel.com/shenang/archive/2009/04/13/265279.html  回復  更多評論   

          導航

          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          agile

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武穴市| 大埔区| 军事| 鄂伦春自治旗| 丰原市| 临安市| 大邑县| 桦南县| 广水市| 通城县| 武宁县| 洛宁县| 潢川县| 馆陶县| 扶风县| 宁陵县| 镶黄旗| 轮台县| 涪陵区| 玉田县| 那坡县| 黄浦区| 施秉县| 浦东新区| 呼图壁县| 宁海县| 开平市| 徐水县| 韶山市| 富蕴县| 江源县| 清流县| 广水市| 茂名市| 龙州县| 正阳县| 湖南省| 公安县| 大连市| 喀喇| 石河子市|