我有一个 Game 类和一个 TestGame 类,我正在尝试从 Game 调用一个方法到 TestGame。
TestGame.java
public class TestGame {
Players ceri = new Players("Ceri", 1);
Players harry = new Players("Harry", 1);
Players lewis = new Players("Lewis", 1);
Players kwok = new Players("Kwok", 1);
Players james = new Players("James", 1);
Players matthew = new Players("Matthew", 1);
Game league = new Game("League Table");
league.addplayer(ceri);
public static void main(String[] args) {
}
}
和Game.java
public class Game {
private String name;
public Game(String name){
this.name = name;
}
public void addPlayer(Players obj){
}
}
出于某种原因,我在 league.addPlayer
下面看到了红线。
最佳答案
league.addplayer(ceri);
是代码,而不是初始化程序。
您需要将其放入方法或初始化 block 内。
在本例中,将其放入您的 main
方法中。
关于java - 无法调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24913530/