我正在用Java开发Stratego游戏,但其中一个游戏部件出现了问题,这引发了错误:
“类型不匹配:无法从常规转换为片断”
我还有其他所有部件,例如炸弹侦察员,马歇尔等。都已定义并且可以正常工作,但是,该gGeneral给了我该错误,我似乎无法理解为什么,因为当我编写代码时,我确实复制从其他棋子粘贴而来的副本,并仅更改其值(用于战斗目的的棋子的整数值)及其图标。
这是其中之一的示例代码:
package Strategu.Pecas;
import javax.swing.ImageIcon;
import Strategu.Piece;
public class Major extends Piece {
public ImageIcon imageRed = new ImageIcon(
"D:\\faculdade\\Modelação e Programação\\code\\MOP\\src\\Strategu\\Gallery\\red\\Major.png");
public ImageIcon imageBlue = new ImageIcon(
"D:\\faculdade\\Modelação e Programação\\code\\MOP\\src\\Strategu\\Gallery\\blue\\Major.png");
ImageIcon image = null;
int value;
public String nome;
public int cx;
public int cy;
public Major(int alliance) {
super(alliance, Cx, Cy);
this.value = 7;
if (alliance == 0)
image = imageRed;
if (alliance == 1)
image = imageBlue;
this.nome = "Major";
}
@Override
public String getNome() {
// TODO Auto-generated method stub
return nome;
}
@Override
public boolean canMoveTo(int x, int y) {// movimento basico de todas as unidades expto o scout
if ((x == Piece.Cx + 1 & y == Piece.Cy) | (x == Piece.Cx & y == Piece.Cy + 1)
| (x == Piece.Cx - 1 & y == Piece.Cy) | (x == Piece.Cx & y == Piece.Cy - 1)) {
return true;
} else {
System.out.println("movimento invalido");
}
return false;
}
@Override
public ImageIcon getIcon() {
// TODO Auto-generated method stub
return image;
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
这是通用文章,我不知道为什么我会因类型不匹配错误而给我带来很多麻烦
package Strategu.Pecas;
import javax.swing.ImageIcon;
import Strategu.Piece;
public class General extends Piece {
public ImageIcon imageRed = new ImageIcon(
"D:\\faculdade\\Modelação e Programação\\code\\MOP\\src\\Strategu\\Gallery\\red\\General.png");
public ImageIcon imageBlue = new ImageIcon(
"D:\\faculdade\\Modelação e Programação\\code\\MOP\\src\\Strategu\\Gallery\\blue\\General.png");
ImageIcon image = null;
int value;
public String nome;
public int cx;
public int cy;
public General(int alliance) {
super(alliance, Cx, Cy);
this.value = 9;
if (alliance == 0)
image = imageRed;
if (alliance == 1)
image = imageBlue;
this.nome = "General";
}
@Override
public String getNome() {
// TODO Auto-generated method stub
return nome;
}
@Override
public boolean canMoveTo(int x, int y) {// movimento basico de todas as unidades expto o scout
if ((x == Piece.Cx + 1 & y == Piece.Cy) | (x == Piece.Cx & y == Piece.Cy + 1)
| (x == Piece.Cx - 1 & y == Piece.Cy) | (x == Piece.Cx & y == Piece.Cy - 1)) {
return true;
} else {
System.out.println("movimento invalido");
}
return false;
}
@Override
public ImageIcon getIcon() {
// TODO Auto-generated method stub
return image;
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return value;
}
}
我也将作品的抽象类放在这里:
package Strategu;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import Strategu.Place.Spot;
import Strategu.board;
public abstract class Piece {
protected final int alliance; // 0 é vermelho 1 é azul
public static int Cx;
public static int Cy;
protected Piece(int alliance, int x, int y) {
this.alliance = alliance;
this.Cx = x;
this.Cy = y;
}
public void setPieceAtPlace(Place p) {
Piece peça = this;
int x = p.coordinateX;
int y = p.coordinateY;
if (p instanceof Spot) {
board.tab[x][y] = new Place.Spot(x, y, peça);
System.out.println("piece adicionada");
}
}
public void setCxCy(int n, int y) {
this.Cx = n;
this.Cy = y;
}
public void sendToGrave(Piece p) {
if (p.alliance == 1) {
Graveyard.sendToGrave(p);
} else {
Graveyard.sendToGrave(p);
}
}
public abstract String getNome();
public abstract boolean canMoveTo(int x, int y);
// public abstract Piece moveTo();
public abstract ImageIcon getIcon();
public abstract int getValue();
public String toString() {
String Ali;
if (this.alliance == 0) {
Ali = "RED";
} else {
Ali = "BLUE";
}
return Ali + " " + getNome() + " " + "value:" + getValue() + " x:" + Piece.Cx + " y:" + Piece.Cy;
}
}