码迷,mamicode.com
首页 > 其他好文 > 详细

深入static与多态

时间:2016-04-10 21:29:02      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

static 隐藏效果.  静态覆盖静态 父亲覆盖儿子

覆盖: 儿子覆盖父亲

static 静态方法的绑定在编译时候已经好了, 多态无效果

class Father 
{ 
public static void print() 
{ 
System.out.println("Father static method called"); 
} 
} 
class Son extends Father 
{ 
public static void print() 
{ 
System.out.println("Son static method called"); 
} 
public static void main(String[] args) 
{ 
Son.print(); 
Father s=new Son (); 
s.print(); 
} 
} 

  输出的结果是:

Son static method called
Father static method called

static 由于静态代码段先编译,实现的是隐藏效果.
隐藏和覆盖的有区别的.


多态其实包括: 覆盖,重写,隐藏.



---恢复内容结束---

static 隐藏效果.  静态覆盖静态 父亲覆盖儿子

覆盖: 儿子覆盖父亲

static 静态方法的绑定在编译时候已经好了, 多态无效果

class Father 
{ 
public static void print() 
{ 
System.out.println("Father static method called"); 
} 
} 
class Son extends Father 
{ 
public static void print() 
{ 
System.out.println("Son static method called"); 
} 
public static void main(String[] args) 
{ 
Son.print(); 
Father s=new Son (); 
s.print(); 
} 
} 

  输出的结果是:

Son static method called
Father static method called

static 由于静态代码段先编译,实现的是隐藏效果.
隐藏和覆盖的有区别的.


多态其实包括: 覆盖,重写,隐藏.

深入static与多态

标签:

原文地址:http://www.cnblogs.com/ziq711/p/5375005.html

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