java-使用抽象创建对象失败,可能是我看不到的简单修复
作者:互联网
关于抽象和构造函数的新手问题.我觉得我缺少明显的东西.
我有一个抽象类Piece,这是构造函数:
public abstract class Piece {
private int[] location = new int[2];
private final char color;
public Piece(char color, int[] location) {
this.location = location;
this.color = color;
}
}
我有一个扩展Piece的类:
public class Bishop extends Piece{
public Bishop(char color, int[] location) {
super(color, location);
}
}
我正在尝试测试.但是,以下代码给出错误:表达式的非法开头,类型的非法开头,缺少“;”
public class testing {
Piece blackBishop = new Bishop('b', {0,0});
}
所有这些文件都在同一包中.在寻找解决方案数小时后,我决定寻求帮助.请让我知道我做错了什么.
解决方法:
{0,0}仅在声明一个int数组变量(int [] var = {0,0};)时才能单独使用.
更改
Piece blackBishop = new Bishop('b', {0,0});
至
Piece blackBishop = new Bishop('b', new int[] {0,0});
标签:abstraction,constructor,java,object 来源: https://codeday.me/bug/20191028/1950662.html