静态引用多个类

时间:2018-11-30 04:02:28

标签: java static

简而言之,我正在尝试学习如何使用更多的类,getter和setter以及正确使用static。有了这个,我决定下棋。

counts
  

主班

================================================================================
Don't use GUI mode for load testing !, only for Test creation and Test debugging.
For load testing, use NON GUI Mode:
   jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]
& increase Java Heap to meet your test requirements:
   Modify current env variable HEAP="-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m" in the jmeter batch file
Check : https://jmeter.apache.org/usermanual/best-practices.html
================================================================================
  

班级

package Package;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class Game {
    private static Color whiteSqaure = new Color(255, 248, 241);
    private static Color whitePiece = new Color(250,230,230);
    private static Color blackSquare = new Color(131, 127, 127);
    private static Color blackPiece = new Color(20,3,3);

    private static Font font = new Font("DialogInput",Font.BOLD,48);

    private static JFrame f = new JFrame();
    private static JPanel grid = new JPanel(new GridLayout(8, 8, 0, 0));
    private static JButton[][] b = new JButton[8][8];

    static Piece[] whitePawns = new Pawn[8];
    static Piece[] whiteRooks = new Rook[2];
    static Piece[] whiteKnights = new Knight[2];
    static Piece[] whiteBishops = new Bishop[2];
    static Piece[] whiteQueen = new Queen[1];
    static Piece[] whiteKing = new King[1];
    static Piece[] blackPawns = new Pawn[8];
    static Piece[] blackRook = new Rook[2];
    static Piece[] blackKnight = new Knight[2];
    static Piece[] blackBishop = new Bishop[2];
    static Piece[] blackQueen = new Queen[1];
    static Piece[] blackKing = new King[1];

    private static void colorButtons() {. . .} 


    private static void addPiecesToBoard() {
        for(int i = 0; i < 8; i++) {
            //String test = "♙";
            String test = Pawn.getWhitePieceIcon();
            b[6][i].setText(test);
        }
    }


    private static void finishFrame() {
        f.add(grid);
        f.pack();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setSize(900, 900);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }


    public static void main(String[] args) {

        colorButtons();
        addPiecesToBoard();
        finishFrame();
    }
}

然后我所有的棋子都上课

现在,我正在尝试将JButton的文本设置为Unicode,我可以轻松做到这一点,但是由于我的静态与非静态问题,我无法通过getter进行处理。我知道可以使用静态方法完成所有操作,但是我想学习如何制作此方法,因此请不要回覆说此方法无效。

我知道我的问题是在getter方法的package Package; abstract class Piece { private String pieceName; private String whitePieceIcon, blackPieceIcon; Piece(String pieceName, String whitePieceIcon, String blackPieceIcon) { this.pieceName = pieceName; this.whitePieceIcon = whitePieceIcon; this.blackPieceIcon = blackPieceIcon; } String getWhitePieceIcon() { return whitePieceIcon; } public void setWhitePieceIcon(String whitePieceIcon) { this.whitePieceIcon = whitePieceIcon; } public String getBlackPieceIcon() { return blackPieceIcon; } public void setBlackPieceIcon(String blackPieceIcon) { this.blackPieceIcon = blackPieceIcon; } public String getPieceName() { return pieceName; } public void setPieceName(String pieceName) { this.pieceName = pieceName; } } 方法的Game类中。我只是不确定如何解决

1 个答案:

答案 0 :(得分:0)

好吧,让我们先看一下这条线

static Piece[] whitePawns = new Pawn[8];

您真正想要的是:

static Piece[] whitePawns = new Pawn[]{new Pawn(), new Pawn(), new Pawn(), ...};

创建一个pawn对象的8个实例,第一个创建一个充满空对象的数组。

此数组是典当对象的8个实例。您定义的pawn对象可以调用抽象类Piece中声明的所有方法。当您以Pawn的形式调用它们时,只能在类Pawn.getWhitePieceIcon()的实例上调用这些方法,而您是在静态调用它,这意味着它不需要完整的Pawn对象就可以存在才能被调用。因此,在这种情况下,您希望像这样创建的典当实例上调用该函数:

private static void addPiecesToBoard() {
    for(int i = 0; i < 8; i++) {
        //String test = "♙";
        String test = whitePawns[i].getWhitePieceIcon();
        b[6][i].setText(test);
    }
}

类似地,木板被声明为错误的,它们是由空JButton组成的双数组,您必须实例化它们。

private static JButton[][] b = createBoard();

private static JButton[][] createBoard() {
    JButton[][] b = new JButton[8][8];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            b[i][j] = new JButton();
        }
    }
    return b;
}

我可以运行它,但是不确定您想要的结果,希望对您有所帮助。