Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 面向对象编程_Java_Oop - Fatal编程技术网

Java 面向对象编程

Java 面向对象编程,java,oop,Java,Oop,有人能帮我做以下几件事吗 -我有一个扩展模型的Gomuku类,我需要一个复制构造函数,但不是重新初始化所有内容,我想从Gomuku类调用模型构造函数: //this is Model constructor public Model ( Model other ) { this.w = other.w; this.h = other.h; this.blacksTurn = other.blacksTurn; this.gameStarted =

有人能帮我做以下几件事吗 -我有一个扩展模型的Gomuku类,我需要一个复制构造函数,但不是重新初始化所有内容,我想从Gomuku类调用模型构造函数:

//this is Model constructor   
     public Model ( Model other ) {
    this.w = other.w;
    this.h = other.h;
    this.blacksTurn = other.blacksTurn;
    this.gameStarted = other.gameStarted;
    this.gameOver = other.gameOver;
    this.blackWon = other.blackWon;
    this.whiteWon = other.whiteWon;


    if (other.w <= 0 || other.h <=0) return;
    this.board = new Piece[other.h][other.w];
    this.winningSequence = new ArrayList<>();
    for(int r=0; r<other.h; r++){
        for(int c=0; c<other.w; c++){
            this.board[r][c] = Piece.None;
        }
    }
}

//this is Gomuku 
    public Gomoku ( Gomoku other ){
    //I want to call Model constructor instead of initializing in here.
    //other.winningSequence = new ArrayList<>();
}
//这是模型构造函数
公共模型(模型其他){
此.w=其他.w;
此.h=其他.h;
this.blacksTurn=other.blacksTurn;
this.gameStarted=other.gameStarted;
this.gameOver=other.gameOver;
this.blackWon=other.blackWon;
this.whiteWon=other.whiteWon;

if(other.w假设您在模型中有一个复制构造函数。您需要使用简单的模型构造函数,这在Gooko对象中是可能的,因为Gomoku扩展了模型。请检查

通过调用
super(object)
您引用的是模型构造函数,它接受模型作为参数。因为Gomoku扩展了模型,所以您可以在这里使用Gomoku对象

在模型类中,属性顺序有问题。 行包含

if (other.w <= 0 || other.h <=0) return;

if(other.w您需要调用
super

public Gomoku ( Gomoku other ){
    super((Model) other); // Model constructor instead of initializing in here.
}

子类构造函数也有一个例子来指导你。

我不知道其他人,但是我很困惑,你到底想做什么,你陷入了哪里。如果你没有得到一个像样的答案,请考虑增加更多的细节和代码,包括你自己的尝试来解决这个问题。在这个问题上有什么解释是错误的吗?是与类有关的吗?<代码> GoMoCu扩展了模型< /代码>吗?同时,你也要考虑改进你的问题标题。它应该是对你当前问题的简短而非常翔实的总结,而不是含糊和过于宽泛(你现在的问题)。。根据我的经验,最初吸引合适专家来回答您的问题的两个最重要的关键是您的问题标题和问题标签。您会希望这些都是高质量的。您可以通过单击此链接编辑您的问题:luk2303,yes Gomuku Extended Model。因此,总结一下:请向我们展示您自己创建此结构的尝试祝你好运!
class Model{
private List<?> winningSequence = new ArrazLis<>();

 public Model ( Model other ) {
  // your constructor
 }
} 
  public Model ( Model other ) {
      this.winningSequence = new ArrayList<>(); // use this before if statement
  }
public Gomoku ( Gomoku other ){
    super((Model) other); // Model constructor instead of initializing in here.
}