40分,简单问题---关于抽象类引用

xxh0534 2007-05-06 06:05:14
Soldier 和 Monster 都是抽像类,想随机的产生soldier 和monster 的级别,可这样出错了,大虾进来看看,

Soldier soldier;
switch (tslevel + 1) {
case 1:
soldier = new WeakSoldier(((tslevel + 1) * 10 + 5),
tslevel + 1);
break;
case 2:
soldier = new NormalSoldier(((tslevel + 1) * 10 + 5),
tslevel + 1);
break;
case 3:
soldier = new StrongSoldier(((tslevel + 1) * 10 + 5), tslevel + 1);
}
/**
* 随机产生怪
*/
Monster monster;
switch (tmlevel + 1) {
case 1:
monster = new BabyMonster(((tslevel + 1) * 10), tslevel + 1);
break;
case 2:
monster = new NormalMonster(((tslevel + 1) * 10), tslevel + 1);
break;
case 3:
monster = new FinalMonster(((tslevel + 1) * 10), tslevel + 1);
break;
}

System.out.println("战士级别" + (tslevel + 1) + " " + "战士HP" + soldier.HP);//soldier.hp 出错 说没有定义soldier
System.out.println("怪级别" + (tmlevel + 1) + " " + "怪HP" + monster.HP);//monster.HP 出错,说没有定义monster
...全文
341 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuekun1172006 2007-05-07
  • 打赏
  • 举报
回复
正像zyongsheng83说的,从运行时抛出的错误来看,楼主的错误只是两个变量没有初始化。
你可以在switch语句里加入defalut看看到底是哪里出了错
林二棍子 2007-05-06
  • 打赏
  • 举报
回复
正像zyongsheng83说的,从运行时抛出的错误来看,楼主的错误只是两个变量没有初始化。

但是楼主的代码中还有其它错误:
在第二个switch中,
monster = new ...Monster(((tslevel + 1) * 10), tslevel + 1);
这句中tslevel应该该为tmlevel

造成这个结果的直接原因可能是楼主进行了代码的复制粘贴。之所以要把一个原本很简单的实例化搞的如此复杂,正是由于楼主没有进行正确的抽象。
奋斗并快乐着 2007-05-06
  • 打赏
  • 举报
回复
先不说他设计的有没有问题,并没有把他实例化,只是实例化了他的子类然后上溯到他的类型,就给出的代码没有什么大的问题,能给出的错误也就是soldier和monster 两个变量没有初始化。

还有楼上,抽象类是extends,接口才是implements
overlv 2007-05-06
  • 打赏
  • 举报
回复
告诉楼主我的经验

抽象类是不可以实例化的但是你可以implements它

但是你必须实现它里面的方法
林二棍子 2007-05-06
  • 打赏
  • 举报
回复
楼主抽象错了。

// 生物是一个接口
public interface Creature {
// 所有的生物都有攻击行为
public int getAttackLevel();
// 生物受到攻击
public void attacked(int attackLevel);
// 取得生命值
public int getHP();

public boolean isDead();
}

// 可升级是一个接口
public interface Upgradable {
public void upgrade();
}

Soldier和Monster是Creature的两个实现,也可以先有一个抽象类

public abstract class AbstractCreature implements Creature {

private int hp;

private int creatureLevel;

public AbstractCreature(int hp, int creatureLevel) {
this.hp = hp;
this.creatureLevel = creatureLevel;
}

public int getHP() {
return hp;
}

public int getCreatureLevel() {
return creatureLevel;
}

public void setCreatureLevel(int creatureLevel) {
this.creatureLevel = creatureLevel;
}

public void lostHP(int count) {
this.hp -= count;
if(this.hp < 0) {
this.hp = 0;
}
}

public boolean isDead() {
return this.hp==0;
}
}

public class Soldier extends AbstractCreature implements Upgradable{

public Soldier(int level) {
super(level*10 + 5, level);
}

public int getAttackLevel() {
// 跟生物等级相关,随便你怎么搞
return getCreatureLevel();
}

public void attacked(int attackLevel) {
// 根据自己的等级和攻击等级计算, 也是随便搞
int count = (attackLevel-this.getCreatureLevel()/2) * 5;
lostHP(count);
}

public void upgrade() {
setCreatureLevel(getCreatureLevel() + 1);
}
}

Monster类似,这里就不写了, 如果Monster不能升级不实现Upgradable接口就好了。

实例化:
Soldier soldier = new Soldier(tslevel + 1);
Monster monster = new Monster(tmlevel + 1);

或者:
Creature soldier = new Soldier(tslevel + 1);
Creature monster = new Monster(tmlevel + 1);

这样的好处是:战士可以升级。按楼主的设计,战士从WeakSoldier升级到NormalSoldier,就要新建一个NormalSoldier,然后抛弃原来的对象。

还有soldier.HP是不对的。内部属性不要直接暴露给外部对象。应该用soldier.getHP()
infon 2007-05-06
  • 打赏
  • 举报
回复
要不把Soldier,Monster定义成接口,,要不干脆派生一个类
IhaveGotYou 2007-05-06
  • 打赏
  • 举报
回复
我晕,抽象类是不能实例化的。
再派生一个类再用吧。
奋斗并快乐着 2007-05-06
  • 打赏
  • 举报
回复
不是说没有定义,是说那两个变量没有初始化吧?
Soldier soldier = null;
Monster monster = null;

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧