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

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

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜章县| 舒城县| 澄迈县| 临汾市| 左云县| 延边| 镇雄县| 永兴县| 罗定市| 宕昌县| 寻甸| 博乐市| 洪湖市| 仁布县| 昭苏县| 女性| 天台县| 临江市| 中阳县| 庄浪县| 伽师县| 江安县| 毕节市| 安远县| 金寨县| 白银市| 民和| 四会市| 建阳市| 临洮县| 屏南县| 天全县| 二连浩特市| 赞皇县| 汶上县| 道孚县| 曲水县| 呼玛县| 庆安县| 通化市| 兰西县|