在Java中覆盖扩展数组大小。

huangapple go评论78阅读模式
英文:

Override extended array size in java

问题

我有一个名为LargeCell的类,它是Cell类的扩展。我想在这两个类中都使用相同的固定数组名称,但数组大小不同。在Cell类中,我希望大小为1,在LargeCell类中,我希望大小为2。当我尝试将囚犯添加到LargeCell时,囚犯实际上被添加到了Cell类中的数组中。我可以覆盖addPrisonerToCell()方法,但这似乎不是最佳解决方案,因为它是从其他类中复制粘贴过来的。有没有办法让方法在正确的类上工作,而无需覆盖它们?

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;
    }
}
英文:

I have a class LargeCell which is an extension of the Cell class. I want to use same name for fixed array in both classes with a different array size. In cell class I want the size to be 1 and in LargeCell class I want size to be 2. When I try to add prisoner to LargeCell instead of prisoner being added to LargeCell, he is added to array in Cell class. I can override addPrisonerToCell() method but it does not seem like the best solution to me since it is copy paste from other class.
Is there any way, to make methods work on the right class without overriding them?

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&lt;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&lt;this.prisoners.length;i++){
        if(this.prisoners[i]==null){
            this.prisoners[i]=prisoner;
            return true;
        }
    }
    return false;
}}

答案1

得分: 2

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

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

	protected Cell(int cellNumber, int size){//只能被子类访问
		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);
	}
}
英文:

By declaring another prisoners field, you are only shadowing the field from the superclass. You could have the Cell constructor accept an int specifying the size of the array like so:

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&lt;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);
	}
}

答案2

得分: 0

我认为你没有充分利用继承的潜力 在Java中覆盖扩展数组大小。 。你在 CellLargeCell 中都声明了 cellNumberPrisoners[] 数组。你可以简单地将它们标记为受保护的,以便让子类(在这种情况下是 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 中重写这个方法以返回不同的值:

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"));
}

这将返回以下输出:

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
英文:

I think you're not using inheritance to it's full potential 在Java中覆盖扩展数组大小。 . You are declaring cellNumber and Prisoners[] array in both Cell and LargeCell. You can simply mark it as protected let them be reused by child class(LargeCell in this case). In order to override size of the array, I introduced a method called getCellSize() which would be called in constructor for array initialization:

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&lt;this.prisoners.length;i++){
            if(this.prisoners[i]==null){
                this.prisoners[i]=prisoner;
                return true;
            }
        }
        return false;
    }
}

You can simply override this method in LargeCell to return a different value:

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&lt;this.prisoners.length;i++){
            if(this.prisoners[i]==null){
                this.prisoners[i]=prisoner;
                return true;
            }
        }
        return false;
    }
}

I tested it like this:

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

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

This returns the following output:

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

huangapple
  • 本文由 发表于 2020年10月14日 23:01:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64356153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定