所以,這可能是也可能不是你想象的,我並不完全確定我已經正確地解決了這個問題,但是我們開始吧!我正在製作一款國際象棋遊戲,我正試圖通過如何稱呼我的課程,製作作品以及將它們放在電路板上來充滿活力。使用一個變量來調用一個新類的實例
運行在代碼上:我讀取了一個文件,其中包含諸如「pla1」之類的代表片段,顏色,x座標和y座標的文件。在本例中,板上的p = pawn,l = white,a = x座標,1 =板上的y座標。所以:
「白色的棋子是A1」
簡單。現在,我有一個能夠從文件中解析文件的小塊類,並且有一個名爲「典當」的特定類,所以當我想爲板添加一塊時,該塊有自己的類來管理它的移動能力和權力棋盤等。
問題:我不知道如何動態設置我的作品類。我可以使用硬編碼版本輕鬆完成此操作,就像您在註釋中看到的或if語句一樣。是否可以用我的字符串變量設置我的類?
我的代碼如下:
//reads from the file, puts each line into an arraylist
public void readFromFile(String fileName) throws IOException
{
String line;
BufferedReader br = new BufferedReader(new FileReader(fileName));
//adds line to arraylist
while((line = br.readLine()) != null)
fileLines.add(line.toLowerCase());
//send each line in the list to method
for(String item : fileLines)
buildPiecesFromFileAndGetLocation(item);
}
ChessPiece[][] pieces = new ChessPiece[9][9]; my chessboard
String spawnPiece;
String spawnColor;
String pieceRepresentation;
String originx;
int originX;
int originY;
public void buildPiecesFromFileAndGetLocation(String item)
{
//regex matcher // using this to match it: Pattern copyPiece = Pattern.compile("(?<piece>q|k|b|p|n|r+)(?<color>l|d)(?<x>\\w)(?<y>\\d)");
Matcher copyMatcher = copyPiece.matcher(item);
//hashmap for matching quicker; currently only has pieceMatches.put("p", "Pawn") within it
hashIdentities();
if (copyMatcher.find())
{
spawnPiece = pieceMatches.get(copyMatcher.group("piece")); //spawnPiece becomes "Pawn"
spawnColor = colorMatches.get(copyMatcher.group("color"));
pieceRepresentation = spawnPiece + spawnColor;
originx = copyMatcher.group("x"); //change letter to number
transferChars(); //changes letter "a" into integer "1"
originY = Integer.parseInt(copyMatcher.group("y")); //string to int
//PROBLEM:
pieces[originX][originY] = new spawnPiece(pieceRepresentation); //since spawnPiece is now "Pawn" i want it to be able to call a new instance of the class Pawn. this way doesn't work. Solution?
//logic:
//pieces[a][b] = new (WHATEVER PIECE WAS MATCHED)("position + color");
//hardcoded version:
//pieces[1][1] = new Pawn("PawnWhite");
}
}
我不知道這是可以做到的。任何幫助將不勝感激,如果需要對代碼進行詳細說明,我可以提供。
你說的「硬編碼」好像是某種邪惡......而「動態地做這個或那個」似乎是某種最佳實踐。 Java是一種靜態類型的語言。如果您試圖設計出無視這一事實的解決方案,那麼您正在努力嘗試,並且最終會導致代碼錯誤。國際象棋遊戲充滿了靜態類型的數據(這些片段在編譯時都是已知的)。我發現你堅持認爲你必須「動態地」這樣做(這樣你就沒有利用conpile時間類型檢查和安全性)是荒謬的。 – scottb
呃,它只是我發現有用的東西,它是OOP的一個重要組成部分,能夠動態地完成它。我寧願用動態的方式來縮短我的代碼。 – user2453973
然後,不同的語言會更好地滿足您的需求。 Java中的反射代碼很慢。閱讀和閱讀也非常冗長和醜陋。而且,在包含所有異常處理的時候,您的代碼幾乎總是更長更冗長。當存在靜態類型的解決方案時,避免Java中的反射代碼爲您提供卓越的安全性,更高的類型安全性,卓越的穩健性和可維護性以及更高的性能。使用靜態類型的環境的優點是令人信服的。 – scottb