저는 파이썬에서 자바로 만든 게임을 포팅하려고합니다. 파이썬 버전에서는, 나는 하나의 "클래스"의 모든 메소드와 변수를했고, 플레이어는이 같은 사전과 같다 :다른 클래스의 기본 클래스에서 공용 변수에 액세스
game.py 플레이어의 데이터 값이 다음 별도로 추가됩니다
...
new_player={"name":"","hp":0,...}
players=[]
//to add new player
players.append(new_player.copy())
:
...
players[0]["name"]="bob"
players[0]["hp"]=50
...
자바 버전에서는 Player 개체와 게임의 기본 메서드를 정의하는 데 사용되는 별도의 클래스가 있습니다. 예를 들어
(이 작은 버전)
game.java (리턴 생략)
import java.utils.*;
public class game
{
public static ArrayList<player> players = new ArrayList<player>();
public static ArrayList<String> pdead = new ArrayList<String>();
public static int turn = 0;
public static void main(String[] args)
{
//do stuff
players.add(new player(name));
//do other stuff
}
public static void do_move(move)
{
//some move is selected
players.get(turn).set_hp(10);
//at this point compiler throws error: cannot find symbol
//compiler does not recognize that a player should have
//been added to the players variable
//other stuff
};
};
player.java (리턴 생략)
public class player
{
//arbitrary list of private variables like hp and name
public player(new_name)
{
name = new_name;
//other variables defined
};
public void set_hp(int amount) //Adding hp
{
hp += amount;
};
public void set_hp(int amount,String type) //taking damage
{
mana += amount;
//go through types, armor, etc.
hp -= amount;
};
public void display stats() //displays all player's stats before choosing move
{
//display stats until...
//later in some for loop
System.out.println(players.get(index).get_hp());
//here compiler throws error again: cannot find symbol
//players arraylist is in main class's public variables
//other stuff
};
//other stuff
};
기발한 때 2 개의 클래스가 함께 컴파일되면 주요 변수가 public이고 프로그램 변수가 플레이어의 변수가 정의되어 있기 때문에 프로그램을 실행할 수 있습니다. 그러나 컴파일러는 이것을 인식하지 못하고 클래스를 (동일한 디렉토리에있는) 클래스가 서로 읽지 않기 때문에 객체를 검사하는 동안 배열/arraylist에서 "정의"되지 않으므로 오류가 발생합니다.
컴파일러가 정의한 변수를 어떻게 보냅니 까? 필요한 경우 두 클래스와 최종 파이썬 버전의 현재 작업 버전을 업로드 할 수 있지만 게임을 닫힌 상태로 유지하려고합니다.
편집 : ArrayList를 초기화 고정 sjkm의 답변에 따라
Java에서는'{}'대괄호를';'로 닫을 필요가 없습니다. –