方法的覆盖和隐藏

代码1

public class Test2 {
    public static void main(String[] args) {
        A a = new B();
        a.method();
    }
}

class A {
    public int a = 10;

    public void method() {
        System.out.println(a);
    }
}

class B extends A {
    public int a = 20;

    public void method() {
        System.out.println(a);
    }

}

代码2

public class Test2 {
    public static void main(String[] args) {
        A a = new B();
        a.method();
    }
}

class A {
    public static int a = 10;

    public static void method() {
        System.out.println(a);
    }
}

class B extends A {
    public static int a = 20;

    public static void method() {
        System.out.println(a);
    }

}

 

 

public class Test2 {
    public static void main(String[] args) {
        A a = new B();
    }
}

class A {
    public int a = 10;

    A() {
        show();
    }

    public void show() {
        System.out.println(a);
    }
}

class B extends A {
    public int a = 20;

    B() {
        show();
    }

    public void show() {
        System.out.println(a);
    }
}

 

 

posted @ 2012-07-18 15:51  Carve_Time  阅读(211)  评论(0编辑  收藏  举报