Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教 (derived class 初始化问题)
floweret



Jute User


发贴: 37
积分: 0
于 2002-11-26 11:58 user profilesend a private message to usersend email to floweretsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
class Base {
  int i = 99;
  public void amethod(){ System.out.println("Base.amethod()"+i);}
Base() { amethod(); }
}
public class Derived extends Base{
  int i = -1;
  public static void main(String argv[]){
  Base b = new Derived();
//Derived无构造函数,所以默认构造函数调用super(),即先执行Base的构造函数,
//Base中调用函数amethod(),该amethod()调用的是Derived中的amethod()

System.out.println(b.i); //i为Base中i的值
  b.amethod(); //amethod()调用的是Derived中的amethod
}  
public void amethod(){ System.out.println("Derived.amethod()"+i); }
}
运行结果:Derived.amethod()0
99
Derived.amethod()-1

请问以上我注释部分理解的是否正确,运行结果第一行的i为何是0


why edited on 2003-06-22 01:25

如果这天地最终会消失,不想一路走来的记忆没有你...
作者 answer [Re:floweret]
SimonLei



CJSDN高级会员


发贴: 288
积分: 100
于 2002-11-26 13:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因为在Base的构造函数当中调用了amethod,
而这个实际上会调用Derived的amethod,此时
Derived本身并没有初始化,因此此时i =0;



You may say I am a dreamer, But I am not the only one.
My blog+wiki: http://blogsite.3322.org/
作者 thx,还有一点疑问 [Re:floweret]
floweret



Jute User


发贴: 37
积分: 0
于 2002-11-27 08:44 user profilesend a private message to usersend email to floweretsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢,还有一点疑问,Derived本身既然还未初试化,那 i 此时定义了没有呢?


如果这天地最终会消失,不想一路走来的记忆没有你...
作者 Re:thx,还有一点疑问 [Re:floweret]
SimonLei



CJSDN高级会员


发贴: 288
积分: 100
于 2002-11-27 08:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
floweret wrote:
谢谢,还有一点疑问,Derived本身既然还未初试化,那 i 此时定义了没有呢?


定义是你编译好了class就定义了的。
而没有初始化,是指没有执行那个赋值操作,
所以缺省值是0



You may say I am a dreamer, But I am not the only one.
My blog+wiki: http://blogsite.3322.org/
作者 Re:请教 [Re:floweret]
floweret



Jute User


发贴: 37
积分: 0
于 2002-11-27 12:02 user profilesend a private message to usersend email to floweretsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
BlushBlushBlush

谢谢



如果这天地最终会消失,不想一路走来的记忆没有你...

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2020 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号
客服电话 0592-8750026    客服信箱 714923@qq.com    客服QQ 714923