Прежде чем я подключил файлы, это как работал мой основной файл (HopeTrail).
Я настроил его так, потому что мне нужно, чтобы мой метод startGame был нестатическим, поскольку он прикреплен к ряду нестатических методов, которые позволяют игре развиваться.< /p>
Код: Выделить всё
public class HopeTrail
{
public static void main(String[] args)
{
HopeTrail game = new HopeTrail();
//new instance of the class HopeTrail
game.startGame();
}
public void startGame()
{
System.out.println("The game starts");
introduction();
//introduction is a method in HopeTrail that begins the game
}
public void introduction()
{ ... }
}
Я быстро понял, что основной файл будет не компилировался, когда я пытался это сделать:
Код: Выделить всё
public class HopeTrail
{
HopeIntro intro = new HopeIntro();
//this is connecting the HopeIntro file to my main file
public static void main(String[] args)
{
HopeTrail game = new HopeTrail();
//new instance of the main file HopeTrail
game.startGame();
}
public void startGame()
{
System.out.println("The game starts");
intro.introduction();
//introduction is now in the HopeIntro file and I am calling
// it with 'intro' object
}
}
Код: Выделить всё
public class HopeIntro
{
HopeTrail trail = new HopeTrail();
//this is letting HopeIntro view HopeTrail's methods
public static void main(String[] args)
{
//this is HopeIntro main method
}
public void introduction()
{ ... }
}
например, это скомпилировано (потому что новый экземпляр не был объявлен в основном методе), однако это не так. не решу мою проблему.
Код: Выделить всё
public class HopeTrail
{
HopeIntro intro = new HopeIntro();
HopeTrail game = new HopeTrail();
public static void main(String[] args)
{
//intro.introduction(); does not work because:
// "cannot make static reference to the non-static field intro
//and
//game.startGame(); does not work for the same reason
}
}
Будем очень признательны за любые мысли. Спасибо, что нашли время прочитать это.
tldr: я хочу соединить 2 Java-файла и по-прежнему использовать экземпляр моего основного файла для запуска игры.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ual-studio
Мобильная версия