First class(Player.java);班級不穩定
public class Player{
int[] playercash = {1500,1500,1500,1500,1500,1500};
int[] playerinvertory = {0,0,0,0,0,0};
int[][] playeritemlist = new int[6][20];
int[][] playeritemdurability = new int[6][20];
int getCash(int playerid) {
return playercash[playerid-1];
}
void giveCash(int playerid, int add) {
playercash[playerid-1] += add;
}
}
Second Class(Robot.java);
public class Robot {
Player p = new Player();//Call player class
void giveCash(int playerid) {
p.giveCash(1,-3000);
}
}
Main;
public class Main {
public static void main(String[] args) {
Player player = new Player();
Robot robot = new Robot();
player.giveCash(1, -2000);//1500-2000 = -500
System.out.println(player.getCash(1)); //Write -500
robot.giveCash(1);
System.out.println(player.getCash(1)); //Write -500
}
}
也就是說,robot.giveCash
不給現金。我認爲機器人班不叫player
班。我想寫-3500
,但它顯示-500
。
'Robot'是Java中的現有類了。儘量不要使用已存在的類的名稱。其他的例子是一個名爲'String' – XtremeBaumer