如何在具有不同构造函数的方法中创建给定对象类型的对象

问题描述

我的问题:我想用一种方法来创建对象,该方法接收要创建的对象以及要创建的对象。而且每个人在构造函数中都会收到不同的参数

docker commit -a "Author Name" -m "Commit message" container-id/container-name new-image-name

解决方法

在抽象类瓷砖中,我这样写:

public abstract GameTile getNewTile(int x,int y);

在我的图块中

@Override
public Tile getNewTile(int x,int y) {
    return new Floor(x,y); //if the tile is floor
}

当我要创建地砖时:

addTiles(0,12,20,new Floor(0,0));

我的方法如下:

public void addTiles(int x,int y,int filas,int columnas,Tile tile){
    for(int i=0; i<filas; i++){
        for(int j=0; j<columnas; j++){
            add(tile.getNewTile(x+j*tile.getWidth(),y+i*tile.getHeight()));
        }
    }
}

如果找到未创建此抽象方法的解决方案,请写下来。谢谢!