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

您没有登录

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

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

赤色彗星



发贴: 74
积分: 2
于 2005-08-04 12:26 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()
{
add(1);
}
void add(int v)
{
i+=v;
}
void print()
{
System.out.println ( i );
}
}

class Extension extends Base
{
Extension()
{
add(2);
}
void add(int v)
{
i+=v*2;
}

}

public class Test
{
public static void main(String args[])
{
bogo(new Extension());
}
static void bogo(Base b)
{
b.add( 8 ) ;
b.print();
}
}

由于是道选择题,书上只有正确答案,但是没分析,我还没入门,所以想请问,上面程序答案的22是怎么得到的. 单看类和方法概念好象是懂了,一联系实际就......-_-!!!!


truthawp edited on 2005-08-05 11:55

作者 Re:超菜问题,关于类和方法 [Re:truthawp]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-04 13:20 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
follow the program flow with a debugger...


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
yqt





发贴: 8
积分: 0
于 2005-08-04 15:35 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
我也想知道是为什么??
谁个給解释解释???



作者 Re:超菜问题,关于类和方法 [Re:why]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-04 20:06 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
why wrote:
follow the program flow with a debugger...

-_-! I'm too "菜" to follow the program flow~ 我想,如果我能用这样的办法解决的话,我应该也不会问出这么菜的问题了~
高手给我一个提示就可以了,比如一个算式(按照程序),最后的结果=22,然后我自己琢磨看看



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-04 20:24 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
truthawp wrote:
-_-! I'm too "菜" to follow the program flow~ 我想,如果我能用这样的办法解决的话,我应该也不会问出这么菜的问题了~
高手给我一个提示就可以了,比如一个算式(按照程序),最后的结果=22,然后我自己琢磨看看

With a debugger, you could for sure follow the flow step by step, well, line by line.
Tell us what you still don't understand after that.



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-04 22:28 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
见笑了,请问DEBUGGER是一个工具还是DOS下的DEBUG命令?


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-04 22:57 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
truthawp wrote:
见笑了,请问DEBUGGER是一个工具还是DOS下的DEBUG命令?

Most, if not all IDEs come with a debugger.

or you may use, not recommended though, the command line one that comes with JDK: jdb
http://java.sun.com/products/jpda/doc/soljdb.html



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-04 23:27 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
恩 谢谢 我会慢慢研究的~~ Smile


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
zhangp_happy





发贴: 22
积分: 0
于 2005-08-05 14:50 user profilesend a private message to usersend email to zhangp_happysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个问题应该这样理解:
首先main中 new Extension()Smile
这样其实就调用了父类的构造方法:Base(),这时i=0,然后add(),i=1+1,
接着调用子类的构造方法:Extension(),这时i=2,然后add() (子类override的方法),
i=2+2*2=6
实例 b调用了子类的add()方法,此时i=6,然后i=6+8*2=22;调用父类的print()
将答案打印出来。
不知道我讲的清不清楚,懂这个意思就好了。Thumbs up



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
yqt





发贴: 8
积分: 0
于 2005-08-05 16:30 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(),这时i=0,然后add(),i=1+1,
按楼上的理解应是这个方法
void add(int v)
{
i+=v;
}
请问楼上
i=1+1
1 是从那的来的???



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-05 18:43 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
恩,基本上理解了 Smile
关于1是从哪里来的问题,我想是调用Base()方法里的add(1)赋了1这个初值(若不对,请前辈们指正我的概念错误)
还有就是main()方法里的几句我还是不太明白
bogo(new Extension());这句是创建Extension的实例吧,用bogo来调用;那这句话可不可以写成Extension bogo=new Extension();Stupid(因为书上都是这样的(是不是这样是比较规范的),突然看到原句的时候我根本就不知道这句是干什么用的,原句的bogo看上去象方法,但是改后的bogo看上去就象对象,那样改等价吗?)
另外,static void bogo(Base b)中,如果bogo()是方法的话,为什么我在原程序里找不到方法体?(为空?那么bogo()方法怎么实现呢?);还有Base b这个b我也感到奇怪,它到底算什么?从后面的程序看应该是对象,如果是,为什么没有实例化就可以用b.add( 8 )和b.print()呢?
以上发言若有错误,还请不吝赐教~ Smile

感谢zhangp_happy和why前辈对我帮助,虽然why前辈教我的方法可能是一劳永逸的,但是对目前的我来说, 还是zhangp_happy前辈的分析更能另我茅塞顿开 Smile



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
dabing





发贴: 2
积分: 0
于 2005-08-05 18:55 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()时,base()中的add() 方法调用的是子类的add()方法。在中个过程中,Test类的add()方法被调用了两次,执行结果为1*2+2*2+2*8=22


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-05 20:42 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
又糊涂了~Stupid
我试着把子类的中add()方法*2给改成*3,发现dabing前辈的算法正确
1*3+2*3+8*3=33;(33是正确结果)
而zhangp_happy前辈的算法
1+1+2*3+8*3=32;就不对了.......
就是说父类的add()方法从头到尾没被调用过(因为被子类的add()重写了,如果要调用父类的方法应该用super吧;而Base()并没有被重写,所以就继承下来了),子类方法在new Extension()时被调用两次:i=i+1*3=3,i=i+2*3=9{int i的初值默认没设置的话应该是0吧};然后在b.add( 8 )时被调用最后一次:i=i+8*3=33
这样的分析没问题吧? 请各位指正~ Smile
还有bogo(new Extension());和static void bogo(Base b)我还是不懂,bogo和b到底是方法还是对象或者是别的什么?



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
zhangp_happy





发贴: 22
积分: 0
于 2005-08-06 10:04 user profilesend a private message to usersend email to zhangp_happysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我认为dabing朋友的思路是正确的,我没考虑到父类的构造方法调用了子类的add()
还有truthawp朋友别称我是前辈,我也是新手,只是交流交流。Embaressed
bogo我认为是test类的一个方法。
b是类Extends的一个实例,并作为bogo的参数。
Base b = new Extends();
Wink



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
meteorping





发贴: 2
积分: 0
于 2005-08-06 11:33 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
dabing的思路是完全正确的...~~


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-06 11:54 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
恩 彻底懂了,说也奇怪,那个bogo方法其实应该很容易看的(现在看看就是一个普通的方法),我不知怎么的,脑子就是卡住了,这里怎么看都看不懂Embaressed
谢谢各位前辈的帮助 Smile



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
acl2005





发贴: 54
积分: 0
于 2005-08-06 16:00 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
大家好,我也是个新手看了半天弄懂点了.就是想知道变量"i"的初值在什么程序里面都是0吗?
还有在那里能看出来调用父类的构造方法BASE()时,BASE()中的ADD()是调用了子类中的ADD()啊?
谢谢!!!!



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2005-08-06 18:03 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
如果是属性 当然在对象初始化的时候 赋值 为0,如果是变量 就不是,自己赋初值

jameszhang edited on 2005-08-06 18:07

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:超菜问题,关于类和方法 [Re:truthawp]
acl2005





发贴: 54
积分: 0
于 2005-08-07 07:14 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
那么楼上的大哥“i”的前面有int整形变量那么他没有给他赋值啊?
谢谢回复!!!Smile



作者 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
*/



阿诺大笨笨
作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-08 11:36 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()中的add()方法,发现这个方法被Extension()中的add()重写,所以就调用子类的add()方法,然后又在Extension()中调用了一次,最后在bogo()方法时再调用了子类add()方法Smile
还有请问javalean前辈,这些注释是怎么弄出来了,如果我会,可能就不会这么麻烦大家了(这个是why前辈所说的Debugger?)Embaressed



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
javalean





发贴: 20
积分: 0
于 2005-08-08 12:15 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
呵呵。我也是初学者。
你看我的代码,就是用了很简单的打印语句啊。
至于java的debugger我也还没有学会,当然专业的测试应该是那样子的。
不过用一些打印语句似乎是初学者的最好选择。
共同努力,学好java。



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-08 16:39 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
恩 这是个好方法~Smile 受益良多


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-08 19:31 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1. It's usually vvvvvvvery easy to use the debugger interface of an IDE.

2. Usually we use a logger instead of System.out.println... anyway, the idea is similar.



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
acl2005





发贴: 54
积分: 0
于 2005-08-08 23:30 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
楼上的朋友说的都很有道理.我也是茅塞顿开啊!!Big Smile


作者 Re:超菜问题,关于类和方法 [Re:truthawp]
飞翔的梦想





发贴: 4
积分: 0
于 2005-08-10 08:55 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
不错 是一个不错的问题 我也明白了好多。


作者 Re:超宋侍?关于类和方法 [Re:truthawp]
andy04911





发贴: 12
积分: 0
于 2005-08-11 00:50 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 b = new Extension( );就调用了两次子类中的add( )方法.

可是我还有点概念上的不明白,Base b//就要使用一次add( )
然后new Extension( )//又要使用一次add( )

那如果实例化子类生成一个对象,如:
Extension c = new Extension( );岂不是也要Extension c//一次
然后new Extension( );一次??
可是事实上实例话对象只会调用一次构造函数啊,可是原题调用了两次.....//晕~

我刚学java,超级菜鸟,可能看书不够仔细有概念遗漏,请前辈们点拨一二,谢谢.^_^



作者 Re:超宋侍?关于类和方法 [Re:andy04911]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2005-08-11 19:14 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
andy04911 wrote:
Base b = new Extension( );就调用了两次子类中的add( )方法.

可是我还有点概念上的不明白,Base b//就要使用一次add( )
然后new Extension( )//又要使用一次add( )

那如果实例化子类生成一个对象,如:
Extension c = new Extension( );岂不是也要Extension c//一次
然后c一次??
可是事实上实例话对象只会调用一次构造函数啊,可是原题调用了两次.....//晕~

我刚学java,超级菜鸟,可能看书不够仔细有概念遗漏,请前辈们点拨一二,谢谢.^_^


new Extension( ); 这个时候才会调用



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:超菜问题,关于类和方法 [Re:truthawp]
ch_zh_80





发贴: 12
积分: 0
于 2005-08-14 10:38 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
那是不是说父类和子类的方法一样时,调用时只调用子类的方法,而父类的方法会被屏蔽.
还有就是,在实例化一个对象时(父类的),是不是自动调用父类的构造函数和子类的构造函数?



作者 Re:超菜问题,关于类和方法 [Re:truthawp]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-14 12:31 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
我觉得再这样纯粹讨论理论影象就不深了,你可以自己试着改写方法体或是别的什么,我也是菜鸟,反正我就自己瞎改,改了方法体,改了名字,然后运行;看看有什么不一样的结果Smile
或是用WHY前辈所说的DEBUGGER来查看



go to first page go to previous page  1   2  go to next page go to last page

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