所以我正在编写游戏 War,我需要将所有方法放在主类中。由于某些错误,我无法初始化变量。
public class Main {
public Player<Card> player1 = new Player<Card>();
public Player<Card> player2 = new Player<Card>();
public Player<Card>[] players = new Player[2];
players[0] = player1;
players[1] = player2;
public static void main(String[] args) {
}
}
在这些代码行中,在创建数组后,我收到一条错误消息: "这条线有多个标记
类型安全:类型 Player[] 的表达式需要未经检查的转换以符合 Player[]
token “;”的语法错误,{应在此 token 之后“
Player 类正在扩展 Card 类型的堆栈。 欢迎提出任何建议。
谢谢。
最佳答案
语句如
players[0] = player1;
players[1] = player2;
不能在方法或构造函数之外完成。
您可以将它们放在构造函数中:
public Main () {
players[0] = player1;
players[1] = player2;
}
关于java - 主类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29861032/