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

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:超菜问题,关于类和方法 [Re:truthawp]
javalean





发贴: 20
积分: 0
于 2005-08-07 16:01 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
//我在楼主的程序里面加了注释,得到了下面的结果
//只是我对为什么会出现这样的结果感到不了解
//我还没有理解好继承的概念:)

class Base
{
int i;
Base()
{
     System.out.println("call Base constructor");
add(1);
}
void add(int v)
{
     System.out.println("call add method in Base");
i+=v;
}
void print()
{
System.out.println ( i );
}
}

class Extension extends Base
{
Extension()
{
     System.out.println("call Extension constructor");
add(2);
}
void add(int v)
{
     System.out.println("call add method in Extension");
i+=v*2;
}

}

public class Test
{
public static void main(String args[])
{

//Extension ex=new Extension();
bogo(new Extension());//1
bogo(ex);//2
}
static void bogo(Base b)
{
     System.out.println("call bogo method");
b.add( 8 ) ;
b.print();
}
}

/*将编号为2处注释掉后的输出结果:
call Base constructor
call add method in Extension
call Extension constructor
call add method in Extension
*/
/*编号为1和2都没有被注释掉,即原程序的执行结果:
call Base constructor
call add method in Extension
call Extension constructor
call add method in Extension
call bogo method
call add method in Extension
22
*/



阿诺大笨笨

话题树型展开
人气 标题 作者 字数 发贴时间
23793 超菜问题,关于类和方法 truthawp 718 2005-08-04 12:26
21000 Re:超菜问题,关于类和方法 why 42 2005-08-04 13:20
21333 Re:超菜问题,关于类和方法 truthawp 205 2005-08-04 20:06
21006 Re:超菜问题,关于类和方法 why 304 2005-08-04 20:24
21043 Re:超菜问题,关于类和方法 truthawp 409 2005-08-05 20:42
20969 Re:超菜问题,关于类和方法 meteorping 20 2005-08-06 11:33
20938 Re:超菜问题,关于类和方法 truthawp 85 2005-08-06 11:54
21445 Re:超菜问题,关于类和方法 acl2005 108 2005-08-06 16:00
20894 Re:超菜问题,关于类和方法 jameszhang 39 2005-08-06 18:03
21474 Re:超菜问题,关于类和方法 acl2005 43 2005-08-07 07:14
21197 Re:超菜问题,关于类和方法 javalean 1364 2005-08-07 16:01
20842 Re:超菜问题,关于类和方法 truthawp 193 2005-08-08 11:36
21185 Re:超菜问题,关于类和方法 javalean 110 2005-08-08 12:15
20781 Re:超菜问题,关于类和方法 truthawp 16 2005-08-08 16:39
21297 Re:超菜问题,关于类和方法 yqt 25 2005-08-04 15:35
20803 Re:超菜问题,关于类和方法 why 164 2005-08-08 19:31
21366 Re:超菜问题,关于类和方法 acl2005 25 2005-08-08 23:30
21175 Re:超菜问题,关于类和方法 飞翔的梦想 21 2005-08-10 08:55
21031 Re:超宋侍?关于类和方法 andy04911 304 2005-08-11 00:50
20766 Re:超宋侍?关于类和方法 jameszhang 360 2005-08-11 19:14
20906 Re:超宋侍?关于类和方法 whyuaou 470 2005-08-15 12:43
20441 Re:超菜问题,关于类和方法 why 134 2005-08-22 07:29
21086 Re:超菜问题,关于类和方法 ch_zh_80 85 2005-08-14 10:38
20745 Re:超菜问题,关于类和方法 truthawp 106 2005-08-14 12:31
21116 Re:超菜问题,关于类和方法 ch_zh_80 59 2005-08-15 11:31
21159 Re:超菜问题,关于类和方法 hxz5830 213 2005-08-16 15:48
20901 Re:超菜问题,关于类和方法 whyuaou 369 2005-08-17 15:22
21062 Re:超菜问题,关于类和方法 zj1290 331 2005-08-20 13:34
21247 Re:超菜问题,关于类和方法 truthawp 34 2005-08-04 22:28
20950 Re:超菜问题,关于类和方法 why 258 2005-08-04 22:57
20948 Re:超菜问题,关于类和方法 brianlove 52 2005-08-22 01:48
21061 Re:超菜问题,关于类和方法 zj1290 23 2005-08-22 10:38
21331 Re:超菜问题,关于类和方法 zj1290 160 2005-08-22 15:23
21281 Re:超菜问题,关于类和方法 truthawp 17 2005-08-04 23:27
21466 Re:超菜问题,关于类和方法 zhangp_happy 236 2005-08-05 14:50
21175 Re:超菜问题,关于类和方法 yqt 133 2005-08-05 16:30
21441 Re:超菜问题,关于类和方法 zhangp_happy 158 2005-08-06 10:04
20989 Re:超菜问题,关于类和方法 truthawp 551 2005-08-05 18:43
21150 Re:超菜问题,关于类和方法 dabing 95 2005-08-05 18:55

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-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923