布尔值从非主类转换为主类

时间:2019-12-25 18:35:36

标签: java methods boolean

我有一个返回true或false的布尔方法。 我如何使用返回值并打印它?

现在我主要有这样的东西:

System.out.println("is the boxes are equals?   " + boxy2.equals(boxy1));

我的Box类具有以下equals方法(此类中不能使用toString方法):

public boolean equals(Box other){
    if (_length == other._length && _height == other._height && _width == other._width){
        return true;
    }
    else{
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您已经完成了?

public static void main(String[] args)
{
    Box boxy1 = new Box(1,1);
    Box boxy1 = new Box(1,1);
    System.out.println("Are the boxes are equals? " + boxy2.equals(boxy1)  );
}

public class Box
{
    private length, width;
    public Box(int l, int w)
    {
        _length = l;
        _width = w;
    }

    public boolean equals(Box other){
        if (_length == other._length && _height == other._height && _width == other._width){
            return true;
        }
        else{
            return false;
        }
    }
}

如果您希望获得一些客户输出,则将其用作打印语句:

System.out.println("Are the boxes are equals? " +  (boxy2.equals(boxy1) ? "Yes" : "No" );