Cube类有两个构造函数,一个接受转换为多维数据集的树属性的三个参数,另一个不需要任何参数,因此会创建一个“空”多维数据集。我的问题是一个布尔方法如何检查立方体是否有效或空?有没有办法做到这一点,而不需要检查每个属性?如何检查对象是否为“空”?
class Application {
public static void main(String[] args) {
Cube c1 = new Cube(4, 3, 6);
Cube c2 = new Cube();
System.out.println(isNotEmpty(c1));
System.out.println(isNotEmpty(c2));
}
public static boolean isNotEmpty(Cube cube) {
if (/*cube attributes are NOT empty*/) {
return true;
} else {
return false;
}
}
public static class Cube {
private int height;
private int width;
private int depth;
public Cube() {}
public Cube(int height, int width, int depth) {
this.height = height;
this.width = width;
this.depth = depth;
}
public int getHeight() { return height; }
public int getWidth() { return width; }
public int getDepth() { return depth; }
}
}
'宽度== 0 &&高度== 0 && depth == 0'? – MadProgrammer
为什么isEmpty是Cube类的一种方法? –
你是什么意思[空](https://stackoverflow.com/questions/44937316/how-do-i-check-if-a-object-is-empty)? –