作者lion1116 (...)
站內java
標題[問題]請問這會是繼承的問題嗎?
時間Wed Sep 12 02:53:42 2007
小弟寫了一個小程式如下,
class Basic {
protected byte lvl;
protected int hp;
protected int mp;
public void setLevel(int _lvl){
lvl = (byte)_lvl; //設定lvl的值;
}
public int getHP(){
return hp; //回傳hp的值;
}
public int getMP(){
return mp; //回傳mp的值;
}
}
public class Elf extends Basic{
public Elf(int _con, int _wis){
con = (byte)_con;
wis = (byte)_wis;
hp = (int)(con*1.2f); //計算基本HP;
hp += (int)(lvl*con*0.8f); //此行則計算多少等級時有多少HP;
mp = (int)(wis * 1);
mp += (int)(lvl*wis*0.7f);
}
}
public class user {
public static void main(String args[]){
Elf elf = new Elf(13,12);
elf.setLevel(10); //設定等級為10;
//列印出HP及MP;
System.out.println("HP: "+elf.getHP()
+"\tMP: "+elf.getMP());
}
}
小弟在測試時,只會印出
HP: 15 MP: 12
後來用debug去看,發現lvl一直維持為 0,
但是又看不出哪裡有問題...
可以麻煩高手指點一下嗎??
--
以上這些程式有點長,先感謝您耐心的檢查。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.164.150.214
推 freesamael:你在設定Level之前就把hp跟mp算好了 09/12 03:54
→ freesamael:改了level之後也沒有重新計算hp跟mp 09/12 03:54
推 plokijuh:換lv型態成int試看看 09/12 03:52
推 lion1116:阿...我了解了,所以說我在使用constructor的時候必須同 09/12 11:38
→ lion1116:時設定等級... 09/12 11:40