Java静态方法不能被覆盖

// 静态方法不能被覆盖

 

/*class Super{

    static String name(){

        return "mother";

    }

}

 

class Test02{

    public static void main(String[] args){

        Super s3 = new Super();

        System.out.println(s3.name());

    }

}

*/

 

/*

* resultmother

*/

 

/*

class Super{

    static String name(){

        return "mother";

    }

}

 

class Sub extends Super{

     String name(){

        return "baby";

    }

}

 

class Test02{

    public static void main(String[] args){

        Super s1 = new Sub();

        System.out.println(s1.name());        

    }

}

*/

 

// result: mother

 

/*

class Super{

     String name(){

        return "mother";

    }

}

 

class Sub extends Super{

    static String name(){

        return "baby";

    }

}

 

class Test02{

    public static void main(String[] args){

        Super s1 = new Sub();

        System.out.println(s1.name());        

    }

}

*/

 

// result: mother

 

class Super{

    static String name(){

        return "mother";

    }

}

 

class Sub extends Super{

    static String name(){

        return "baby";

    }

}

 

class Test02{

    public static void main(String[] args){

        Super s = new Super();

        System.out.println(s.name());        

        Super s1 = new Sub();

        /*运行时是指向Sub的类,s1.name()应该输出"baby"的。却输出mother*/

        System.out.println(s1.name());

        /*

         * 结果是:mother

         * "静态方法在编译时根据调用该方法的类或对象所属的类决定

         * 实例方法在运行时根据对象所属的类决定"该如何理解?

         */

    }

}

 

posted @ 2015-09-22 12:42  iucforever  阅读(1147)  评论(0编辑  收藏  举报