A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周志强 中级黑马   /  2013-1-29 22:07  /  1435 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 2013-2-2 11:49 编辑

大侠们看看下面这段程序的运行结果是什么?
  1. class Demo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 new Derived();
  6.         }
  7. }
  8. class Base
  9. {
  10.         int count =2;
  11.         public Base()
  12.         {
  13.                 this.display();
  14.         }
  15.         public void display()
  16.         {
  17.                 System.out.println(count);
  18.         }
  19. }
  20. class Derived extends Base
  21. {
  22.         private int count =20;
  23.         public Derived()
  24.         {
  25.                 count = 22;
  26.         }
  27.         public void display()
  28.         {
  29.                 System.out.println(count);
  30.         }
  31. }
复制代码
看懂了吗?程序的运行结果竟然是:0 。求解释!!!

4 个回复

倒序浏览
因为你在new一个对象的时候,它是先调用的构造函数,后给这个类的变量赋值.所以打印的结果是0了.你再调用一次display方法就知道了.打印的结果是22!
回复 使用道具 举报
我在你代码后面写了注释,你自己看吧
  1. class Demo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Derived d =  new Derived();//第一步.创建一个对象,调用Drived构造方法
  6.                 //d.display();
  7.         }
  8. }
  9. class Base
  10. {
  11.         int count =2;
  12.         public Base()
  13.         {
  14.                 this.display();                //第三步,使用父类构造方法,调用display(),该方法已被子类重写,
  15.                                                         //java的多态,父类引用指向子类对象,但此时子类对象还没有被构造出来(子类构造方法还未调用)
  16.                                                     //所以打印的结果是0
  17.         }
  18.         public void display()
  19.         {
  20.                 System.out.println(count);
  21.         }
  22. }
  23. class Derived extends Base
  24. {
  25.         private int count =20;
  26.         public Derived()
  27.         {                                        //第二步.调用子类的构造方法,这里其实有一个super();创建子类对象前会调用父类无参构造方法
  28.                 count = 22;
  29.         }
  30.         public void display()//父类该方法已经被子类重写
  31.         {
  32.                 System.out.println(count);
  33.         }
  34. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
舒远 + 1 很给力!

查看全部评分

回复 使用道具 举报
class Demo
{
        public static void main(String[] args)
        {
                new Derived();//0
                                new Derived().display();//这句,输出结果是:0,22,0是Base类构造函数里的display输出的,但是这个display是Derived类里的display
                                                                   //因为this 代表调用它的对象,而此时Derived类对象构造函数还没有执行,所以count是默认的0.
                                                                
                                new Base().display();//结果2,2.第一个2是Base类的构造函数里的display输出的,第二个构造函数下面的display输出的。
        }
}
class Base
{
        int count =2;
        public Base()
        {
                this.display();
        }
        public void display()
        {
                System.out.println(count);
        }
}
class Derived extends Base
{
        private int count =20;
        public Derived()
        {
                count = 22;
        }
        public void display()
        {
                System.out.println(count);
        }
}

说明:类实例化的时候,首先调用构造函数,所以你的主函数里的语句结果是类Derived的display()输出的,而这时候还没有赋值。

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
谢谢大家了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马