找回密码
 立即注册
搜索
查看: 2030|回复: 3

[软件] java入门编程向求助

[复制链接]
     
发表于 2011-8-7 17:11 | 显示全部楼层 |阅读模式
  1.     /**
  2.      * check the player is alive.
  3.      *
  4.      * @return     if player is dead, return false. Else alive.
  5.      */
  6.     public boolean isAlive(){
  7.         
  8.         
  9.         if(kill())
  10.         {
  11.             return false;
  12.         } else{
  13.         
  14.             return true;
  15.         }
  16.     }
  17.     
  18.     /**
  19.      * check the player is dead.
  20.      *
  21.      * @return     true.
  22.      */
  23.     public boolean kill(){
  24.         
  25.         
  26.         return true;
  27.     }
复制代码

unit test:
  1.     public void testIsAlive()
  2.     {
  3.         assertTrue(player.isAlive());
  4.     }
  5.     
  6.     public void testKill()
  7.     {
  8.         player.kill();
  9.         assertFalse(player.isAlive());
  10.     }    
复制代码

test后testIsAlive不能通过啊。。。这什么情况啊
逻辑上感觉没问题啊
回复

使用道具 举报

     
发表于 2011-8-7 17:17 | 显示全部楼层
isAlive会先执行kill(),
kill() return true;
当然isAlive return false;
你assertTrue当然通不过 因为player.isAlive()会一直是false
回复

使用道具 举报

     
 楼主| 发表于 2011-8-7 18:03 | 显示全部楼层
  1. private boolean alive = true;
  2.     /**
  3.      * check the player is alive.
  4.      *
  5.      * @return     if player is dead, return false. Else alive.
  6.      */
  7.     public boolean isAlive(){
  8.         
  9.         return alive;
  10.     }
  11.    
  12.     /**
  13.      * check the player is dead.
  14.      *
  15.      *
  16.      */
  17.     public void kill(){
  18.         
  19.         alive = false;
  20.     }
复制代码

这样虽然通过了,但这样真的可以吗?
回复

使用道具 举报

     
发表于 2011-8-7 20:58 | 显示全部楼层
kill弄成kill(Player player)
通过player的setAlive方法把他弄死
然后用player.isAlive检验
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|上海互联网违法和不良信息举报中心|网上有害信息举报专区|962110 反电信诈骗|举报电话 021-62035905|Stage1st ( 沪ICP备13020230号-1|沪公网安备 31010702007642号 )

GMT+8, 2025-9-15 23:50 , Processed in 0.057069 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表