Java 返回更新的对象

Java 返回更新的对象,java,Java,如何从我提供的示例中的BasicRect剪切返回更新的矩形 只是水平切割中的BasicRect出现错误 public class BasicRect implements ADT_BasicRect, Comparable<BasicRect> { private int x, y, h, w; public BasicRect(int x, int y) { this.x = x; this.y = y; h =

如何从我提供的示例中的BasicRect剪切返回更新的矩形

只是水平切割中的
BasicRect
出现错误

public class BasicRect implements ADT_BasicRect, Comparable<BasicRect> {

    private int x, y, h, w;

    public BasicRect(int x, int y) {
        this.x = x;
        this.y = y;
        h = y;
        w = x;
    }

    public int getArea() {
        return h * w;
    }

    @Override
    public BasicRect horizontalCut(int c) {
        /**
         * Provided the cut-value is strictly between 0 and the width of this
         * BasicRect, the width of this is changed to the cut-value. The
         * BasicRect representing the right-hand part of the cut is returned.
         *
         * @param c the cut value
         * @return the right-hand part of the cut If the cut-value is not
         * strictly between 0 and the width of this, an IllegalArgumentException
         * is thrown
         */
        if(c > 0 && c < w){
            // make the cut
            x = x-w;
        }else{
            throw new IllegalArgumentException("Must be smaller than"
                    + " width but larger than 0");
        }
        return BasicRect;
    }

创建一个对象,更新其属性并返回它

public BasicRect horizontalCut(int c) {
    BasicRect br = new BasicRect();
    br.x = x;
    br.y = y;
    br.h = h;
    br.w = w;
    if(c > 0 && c < w){
        // make the cut
        br.x = x-w;
    }else{
        throw new IllegalArgumentException("Must be smaller than"
                + " width but larger than 0");
    }
    return br;
}
public BasicRect horizontalCut(int c){
BasicRect br=新的BasicRect();
br.x=x;
br.y=y;
br.h=h;
br.w=w;
如果(c>0&&c
如果要修改当前对象并返回它,请执行此操作

public BasicRect horizontalCut(int c) {
    if(c > 0 && c < w){
        // make the cut
        x = x-w;
    }else{
        throw new IllegalArgumentException("Must be smaller than"
                + " width but larger than 0");
    }
    return this;
}
public BasicRect horizontalCut(int c){
如果(c>0&&c
如果您想按照下面评论中的要求处理当前对象。我相信
BasicRect
是域对象,已经在某些服务中创建。这就是你需要走的路

public BasicRect horizontalCut(int c) {

    if(c > 0 && c < w){
        // make the cut
        this.x = this.x-w;
    }else{
        throw new IllegalArgumentException("Must be smaller than"
                + " width but larger than 0");
    }
    return this;
}
public BasicRect horizontalCut(int c){
如果(c>0&&c
所以无法更新当前对象?而不是创建一个新的?嗨,伙计,有什么原因我的更新不起作用吗?只是试着测试它(检查我的编辑),我相信它应该是x=x-c而不是-w,这是我的错误什么是“工作”?它应该编译,移动rect不会影响它的区域。
this.x=this.x-this.x
看起来像
this.x=0谢谢。更正打字错误
public BasicRect horizontalCut(int c) {

    if(c > 0 && c < w){
        // make the cut
        this.x = this.x-w;
    }else{
        throw new IllegalArgumentException("Must be smaller than"
                + " width but larger than 0");
    }
    return this;
}