【问题标题】:Using an object as a paramater [duplicate]使用对象作为参数[重复]
【发布时间】:2021-05-12 13:48:45
【问题描述】:

对于我的项目,我必须修改这个方法:

/**
     * Determines if this position equals the specified position
     * 
     * @return true of this position and the specified position have the same row
     *         and column value
     */
    @Override
    public boolean equals(Object pos) {
        return false;
    }

我将如何解决这个问题?我知道要修改代码,但我在理解如何处理 'pos' 作为对象时遇到了问题。

在 Position 类中,有两个方法称为“getRow”和“getCol”。我怎么能这样做?

【问题讨论】:

标签: java object


【解决方案1】:

有很多方法可以做到这一点。这是一个:

public class Position {
    
    private int column;
    private int row;

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Position position = (Position) o;
        return column == position.column && row == position.row;
    }
}

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2018-08-02
    • 1970-01-01
    相关资源
    最近更新 更多