手指的下面
时间长了,手指会痛
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 阅读(83) | 评论 (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 阅读(192) | 评论 (2)编辑 收藏

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

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜