码迷,mamicode.com
首页 > 编程语言 > 详细

Java静态方法不能被覆盖

时间:2015-09-22 14:30:55      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

// 静态方法不能被覆盖

?

/*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

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

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

???????? */

????}

}

?

Java静态方法不能被覆盖

标签:

原文地址:http://www.cnblogs.com/iucforever/p/4828610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!