在 java 中覆盖扩展数组大小

Override extended array size in java

我有一个 class LargeCell,它是 Cell class 的扩展。我想在两个具有不同数组大小的 classes 中为固定数组使用相同的名称。在 class 单元格中,我希望大小为 1,在 LargeCell class 中,我希望大小为 2。当我尝试将囚犯添加到 LargeCell 而不是将囚犯添加到 LargeCell 时,他被添加到数组中在单元格 class 中。我可以覆盖 addPrisonerToCell() 方法,但它对我来说似乎不是最佳解决方案,因为它是从其他 class 复制粘贴的。 有什么方法可以让方法在右边工作 class 而不覆盖它们?

public class Cell {
private int cellNumber;
private Prisoner[] prisoners;


public Cell(int cellNumber){
    this.cellNumber=cellNumber;
    this.prisoners=new Prisoner[1];
}


public boolean addPrisonerToCell(Prisoner prisoner) {
    for(int i=0; i<this.prisoners.length;i++){
        if(this.prisoners[i]==null){
            this.prisoners[i]=prisoner;
            return true;
        }
    }
    return false;
}}


public class LargeCell extends Cell {

private Prisoner[] prisoners;

public LargeCell(int cellNumber) {
    super(cellNumber);
    this.prisoners = new Prisoner[2];

}
@Override
public boolean addPrisonerToCell(Prisoner prisoner) {
    for(int i=0; i<this.prisoners.length;i++){
        if(this.prisoners[i]==null){
            this.prisoners[i]=prisoner;
            return true;
        }
    }
    return false;
}}

通过声明另一个 prisoners 字段,您只是从超类中屏蔽了该字段。您可以让 Cell 构造函数接受一个指定数组大小的 int,如下所示:

public class Cell {
    private int cellNumber;
    private Prisoner[] prisoners;
    public Cell(int cellNumber){
        this(cellNumber, 1);
    }

    protected Cell(int cellNumber, int size){//only accessible by subclasses
        this.cellNumber = cellNumber;
        this.prisoners = new Prisoner[size];
    }


    public boolean addPrisonerToCell(Prisoner prisoner) {
        for(int i=0; i<this.prisoners.length;i++){
            if(this.prisoners[i]==null){
                this.prisoners[i]=prisoner;
                return true;
            }
        }
        return false;
    }
}

public class LargeCell extends Cell {
    public LargeCell(int cellNumber) {
        super(cellNumber, 2);
    }
}

我认为您没有充分发挥继承的潜力:-)。您在 CellLargeCell 中声明了 cellNumberPrisoners[] 数组。您可以简单地将其标记为受保护,让它们被 child class(在本例中为 LargeCell)重用。为了覆盖数组的大小,我引入了一个名为 getCellSize() 的方法,该方法将在构造函数中调用以进行数组初始化:

public class Cell {
    protected int cellNumber;
    protected Prisoner[] prisoners;

    public Cell(int cellNumber){
        this.cellNumber=cellNumber;
        this.prisoners=new Prisoner[getCellSize()];
    }

    public int getCellSize() { return 1; }

    public boolean addPrisonerToCell(Prisoner prisoner) {
        for(int i=0; i<this.prisoners.length;i++){
            if(this.prisoners[i]==null){
                this.prisoners[i]=prisoner;
                return true;
            }
        }
        return false;
    }
}

您可以简单地将 LargeCell 中的此方法重写为 return 不同的值:

public class LargeCell extends Cell {

    public LargeCell(int cellNumber) {
        super(cellNumber);
    }

    @Override
    public int getCellSize() { return 2; }

    @Override
    public boolean addPrisonerToCell(Prisoner prisoner) {
        for(int i=0; i<this.prisoners.length;i++){
            if(this.prisoners[i]==null){
                this.prisoners[i]=prisoner;
                return true;
            }
        }
        return false;
    }
}

我是这样测试的:

public static void main(String[] args) {
    Cell cell = new Cell(11);
    System.out.println("Adding A to cell ..." + (cell.addPrisonerToCell(new Prisoner("A")) ? "Success" : "Failed"));
    System.out.println("Adding B to cell ..." + (cell.addPrisonerToCell(new Prisoner("B")) ? "Success" : "Failed"));

    LargeCell largeCell = new LargeCell(12);
    System.out.println("Adding C to large cell ..." + (largeCell.addPrisonerToCell(new Prisoner("C")) ? "Success" : "Failed"));
    System.out.println("Adding D to large  cell ..." + (largeCell.addPrisonerToCell(new Prisoner("D")) ? "Success" : "Failed"));
    System.out.println("Adding E to large  cell ..." + (largeCell.addPrisonerToCell(new Prisoner("E")) ? "Success" : "Failed"));
}

这 return 是以下输出:

Adding A to cell ...Success
Adding B to cell ...Failed
Adding C to large cell ...Success
Adding D to large  cell ...Success
Adding E to large  cell ...Failed