手指的下面
          時間長了,手指會痛
          posts - 3,  comments - 2,  trackbacks - 0
          拋出異常時,用printStackTrace()來跟蹤異常信息,會打印最“子”的信息,
          如果不是很有必要,就Exception就OK了

           1public class Demo {
           2    public static void main(String args[]) {
           3        int i = 0;
           4        try {
           5            if (i == 0{
           6                throw new LeafException();
           7            }

           8        }
           catch (TreeException e) {
           9            e.printStackTrace();
          10        }

          11    }

          12}

          13
          14class TreeException extends Exception{
          15    public TreeException () {
          16        System.out.println("TreeException()");
          17    }

          18}

          19
          20class LeafException extends TreeException{
          21    public LeafException () {
          22        System.out.println("LeafException()");
          23    }

          24}

          打印結果:
          TreeException()
          LeafException()
          LeafException
                  at Demo.main(Demo.java:6)
          posted @ 2008-08-19 22:19 xunSir.lee 閱讀(97) | 評論 (0)編輯 收藏
          子類繼承父類
              1:子類方法優先檢索并使用子類的方法
              2:子類與父類的屬性都是存在,引用類型是父類時就使用父類屬性,是子類就使用子類屬性
              3
          :調用父類方法(super.method()),父類方法中:
                  調用方法,優先搜索子類中是否有該方法,然后是父類的;
                  調用屬性,則是使用父類的屬性。
           1class Tree {
           2    int i = 3, j = 5;
           3
           4    public Tree() {
           5        this.show("Tree");
           6    }

           7
           8    protected void show(String s) {
           9        System.out.println(s);
          10    }

          11
          12    public void t() {
          13        this.show("Tree, t");
          14        System.out.println(this.i);
          15        System.out.println(this.j);
          16    }

          17}

          18
          19class Leaf extends Tree {
          20    int i = 10;
          21
          22    public Leaf () {
          23        this.show("Leaf");
          24    }

          25
          26    public void show(String s) {
          27        System.out.println(s + "\t\tLeaf");
          28    }

          29
          30    public void t() {
          31        show("Leaf, t");
          32        System.out.println(this.i);
          33        System.out.println(this.j);
          34        System.out.println();
          35        super.t();
          36    }

          37
          38    public static void main(String args[]) {
          39        Leaf l = new Leaf();
          40        l.t();
          41
          42        System.out.println("------------------");
          43        System.out.println(l.i);
          44        Tree t = new Leaf();
          45        l = (Leaf) t;
          46        System.out.println(t.i);
          47        System.out.println(l.i);
          48    }

          49}

          50


          打印結果:

           

          Tree            Leaf
          Leaf            Leaf
          Leaf, t         Leaf
          10
          5

          Tree, t         Leaf
          3
          5
          ------------------
          10
          Tree            Leaf
          Leaf            Leaf
          3
          10

          posted @ 2008-08-18 22:13 xunSir.lee 閱讀(203) | 評論 (2)編輯 收藏

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 安远县| 新疆| 邵阳市| 江源县| 河北区| 六枝特区| 陵川县| 南阳市| 宜州市| 文山县| 贵州省| 会宁县| 石家庄市| 五华县| 淮滨县| 神农架林区| 河北区| 曲水县| 北碚区| 固始县| 广平县| 榆中县| 清流县| 灵丘县| 高州市| 新巴尔虎左旗| 南昌县| 营口市| 南乐县| 佛教| 广饶县| 航空| 泰来县| 昌乐县| 威远县| 石泉县| 四平市| 万载县| 海阳市| 蓬莱市| 三门县|