手指的下面
          時間長了,手指會痛
          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 閱讀(202) | 評論 (2)編輯 收藏
          僅列出標題  

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 日照市| 拉孜县| 沙雅县| 内黄县| 恩施市| 南涧| 诸城市| 中阳县| 政和县| 昭苏县| 遂平县| 连州市| 阿坝县| 康平县| 射洪县| 手机| 安新县| 宁河县| 庆元县| 北票市| 武强县| 麦盖提县| 贞丰县| 边坝县| 监利县| 宁化县| 武强县| 中江县| 望江县| 潼关县| 尤溪县| 三亚市| 柳河县| 田林县| 肥西县| 高要市| 阿克苏市| 临江市| 沂水县| 蒙山县| 凯里市|