子类与父类static方法调用顺序

来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 11:49 点击:
Java代码
第一次测试: 
 
 父类: 
 
public class Parent { 
     
    protected static void staticmethod(){ 
        System.out.println("parent"); 
    } 
     
    protected void say(){ 
         
        System.out.println("this is parent"); 
    } 
 

 
 
Java代码
子类: 
public class Son extends Parent { 
     
     
    @Override 
    protected void say() { 
        // TODO Auto-generated method stub 
        System.out.println("this is son"); 
    } 
     
    protected static void staticmethod(){ 
        System.out.println("son"); 
    } 
 

 
调用:
Java代码
      Parent son1 = new Son(); 
 
son1.staticmethod(); 
son1.say(); 
 
Son son2 = new Son(); 
 
son2.staticmethod(); 
son2.say(); 
 
Parent p1 = new Parent(); 
 
p1.staticmethod(); 
 
p1.say(); 
 
输出结果:
 
Java代码
parent 
this is son 
son 
this is son 
parent 
this is parent 
 
即为: 父类的 static 方法 子类不能覆盖,但是子类能与父类有相同说明的static方法,
如上:protected static void staticmethod(),
     但是
Java代码
son1.staticmethod() 
输出 
parent 
   同时
Java代码
son2.staticmethod(); 
输出 
son 
 
第二次测试:
 
  修改子类:
Java代码
public class Son extends Parent { 
     
     
    @Override 
    protected void say() { 
        // TODO Auto-generated method stub 
        System.out.println("this is son"); 
    } 
     
    protected static void staticmethod(){ 
        System.out.println("son"); 
    } 
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Java编程语言的八大优点
  • JVM对象生命周期详细介绍
  • Java平台上的CRM系统
  • Java 算数测试小程序
  • Command(命令模式)
  • Java 一个简单的画图程序
  • Java环境 使用Resin在NT环境下配置JSP环境
  • Java 日历的小程序
  • Java 统计代码的小工具
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1