Java 我如何在不扩展类的情况下从一个类到另一个类获取值?
我是编程新手,这可能是个愚蠢的问题,但问题是: 这是我的班级董事会:Java 我如何在不扩展类的情况下从一个类到另一个类获取值?,java,Java,我是编程新手,这可能是个愚蠢的问题,但问题是: 这是我的班级董事会: public class Board { public static final int COLOR_WHITE = 1; public static final int COLOR_BLACK = 2; PlayingPiece[][] board; private boolean isFirstMove; private int color; public Board() { this.setBoard(ne
public class Board {
public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;
PlayingPiece[][] board;
private boolean isFirstMove;
private int color;
public Board() {
this.setBoard(new PlayingPiece[8][8]);
this.isFirstMove = true;
this.initializePieces();
}
// Initialize the chess pieces
public void initializePieces() {
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(1, i, COLOR_WHITE);
}
for (int i = 0; i < 8; i++) {
board[6][i] = new Pawn(6, i, COLOR_BLACK);
}
board[0][0] = new Rook(0, 0, COLOR_WHITE);
board[0][7] = new Rook(0, 7, COLOR_WHITE);
board[7][0] = new Rook(7, 0, COLOR_BLACK);
board[7][7] = new Rook(7, 7, COLOR_BLACK);
board[0][1] = new Knight(0, 1, COLOR_WHITE);
board[0][6] = new Knight(0, 6, COLOR_WHITE);
board[7][1] = new Knight(7, 1, COLOR_BLACK);
board[7][6] = new Knight(7, 6, COLOR_BLACK);
board[0][2] = new Officer(0, 2, COLOR_WHITE);
board[0][5] = new Officer(0, 5, COLOR_WHITE);
board[7][2] = new Officer(7, 2, COLOR_BLACK);
board[7][5] = new Officer(7, 5, COLOR_BLACK);
board[0][3] = new Queen(3, 0, COLOR_WHITE);
board[0][4] = new King(4, 0, COLOR_WHITE);
board[7][3] = new Queen(7, 3, COLOR_BLACK);
board[7][4] = new King(7, 4, COLOR_BLACK);
this.printBoard();
}
public boolean play(int color, int fromX, int fromY, int toX, int toY) {
boolean isTrue = false;
// Check if this is the first turn and only white can move
if (isFirstMove && color == COLOR_WHITE) {
isTrue = true;
} else if (isFirstMove && color == COLOR_BLACK) {
return false;
}
// check if player plays 2 times in a raw and if you move the piece from
// current possition
if (color == this.color || (toX == fromX && toY == fromY)) {
return false;
}
isTrue = true;
if (isTrue == true) {
this.isFirstMove = false;
// Check if player plays with his own color
if (((board[fromX][fromY]).getColor() != color)) {
return false;
}
// Check the isLegal movement of every chess piece
if ((board[fromX][fromY]).move(toX, toY)) {
board[toX][toY] = board[fromX][fromY];
board[fromX][fromY] = null;
}
this.printBoard();
}
return isTrue;
}
public PlayingPiece[][] getBoard() {
return board;
}
public void setBoard(PlayingPiece[][] board) {
this.board = board;
}
好的,接下来是我的另一个棋类课程:
public class PlayingPiece {
public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;
public static final char BLACK_PAWN = '\u265F';
public static final char BLACK_ROOK = '\u265C';
public static final char BLACK_KNIGHT = '\u265E';
public static final char BLACK_BISHOP = '\u265D';
public static final char BLACK_QUEEN = '\u265B';
public static final char BLACK_KING = '\u265A';
public static final char WHITE_PAWN = '\u2659';
public static final char WHITE_ROOK = '\u2656';
public static final char WHITE_KNIGHT = '\u2658';
public static final char WHITE_BISHOP = '\u2657';
public static final char WHITE_QUEEN = '\u2655';
public static final char WHITE_KING = '\u2654';
public static final char NO_PIECE = ' ';
private int x, y;
private boolean isAlive;
private int color;
private char symbol;
protected PlayingPiece (int newX, int newY, int newColor) {
this.setX(newX);
this.setY(newY);
this.color = newColor;
this.isAlive = true;
}
protected PlayingPiece(int newX, int newY) {
this.setX(newX);
this.setY(newY);
}
protected PlayingPiece() {
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
protected boolean moveIsLegal (int newX, int newY) {
boolean isLegal = false;
if ((0 <= newX && newX <= 7) && (0 <= newY && newY <= 7)){
isLegal = true;
}
return isLegal;
}
public boolean move (int newX, int newY) {
if (moveIsLegal(newX, newY)) {
setX(newX);
setY(newY);
return true;
}
return false;
}
public int getColor() {
return color;
}
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public char getSymbol() {
return symbol;
}
public void setSymbol(char symbol) {
this.symbol = symbol;
}
public class PlayingPiece{
公共静态最终整数颜色_白色=1;
公共静态最终整数颜色_黑色=2;
公共静态最终字符黑_PAWN='\u265F';
公共静态最终字符BLACK_ROOK='\u265C';
公共静态最终字符黑骑士='\u265E';
公共静态最终字符黑色\u BISHOP='\u265D';
公共静态最终字符黑色\u皇后='\u265B';
公共静态最终字符黑_KING='\u265A';
公共静态最终字符WHITE_PAWN='\u2659';
公共静态最终字符WHITE_ROOK='\u2656';
公共静态最终字符WHITE_KNIGHT='\u2658';
公共静态最终字符WHITE_BISHOP='\u2657';
公共静态最终字符WHITE_QUEEN='\u2655';
公共静态最终字符WHITE_KING='\u2654';
公共静态最终字符编号=“”;
私有整数x,y;
私人生活;
私人内特色;
私有字符符号;
受保护的播放件(int-newX、int-newY、int-newColor){
这是setX(newX);
这是塞蒂(纽伊);
this.color=newColor;
this.isAlive=true;
}
受保护的播放件(int-newX、int-newY){
这是setX(newX);
这是塞蒂(纽伊);
}
受保护的播放件(){
}
公共int getX(){
返回x;
}
公共空间设置(整数y){
这个。y=y;
}
公共int getY(){
返回y;
}
公共无效集合x(整数x){
这个.x=x;
}
受保护的布尔值moveIsLegal(int-newX,int-newY){
布尔值isLegal=false;
如果我不知道我是否理解你的问题,但是
如果您有两个类A和B,而不是使用继承,则可以使用聚合。例如,您希望使用类A中对象B的某个方法:
class A {
private B b;
}
然后在类A的方法中使用b.NameOfMethod()
另一个解决方案(假设不需要多个板实例)是使板保持静态
public static PlayingPiece[][] board;
然后,您可以使用Board.Board从Pawn类访问它。Board
您可以创建一个方法来返回数组,并在需要数据的类中调用它
public PlayingPiece getBoard()
{
return board;
}
创建
对象(Board类)
-->调用initializePieces()方法
-->访问对象.Board[toX][toY]
我只想使用这个数组字段的值Board[toX][toY]
在Pawn类中我怎么做?你可以在Pawn类中创建Board的实例。然后在Pwn对象中,你可以通过Pawn对象实例访问它。这就是我的意思,非常感谢!我还有一个问题,如何才能等于这个元素Board.Board[toX][toY]
如果它来自于PlayingPiece
请按此方法键入isValidTrace
?对不起,你能重新措辞吗?不太清楚你在这里问什么。当然,对不起:)好的,我会得到这个元素Board.Board[toX][toY]
我想检查一下它是否来自于类型PlayingPiece我怎么能做到这一点呢?如果(Board.Board[newX][newY].equals(new PlayingPiece())
这是否正确?您可以使用instanceof运算符。如果(Board.Board[newX][newY]instanceof PlayingPiece)
编辑:无需担心。instanceof返回false表示null,因此您可以使用它。没问题,就是它!这是null的答案!谢谢:)
public static PlayingPiece[][] board;
public PlayingPiece getBoard()
{
return board;
}