Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在compareTo()方法中实现特定条件_Java_Compareto - Fatal编程技术网

Java 如何在compareTo()方法中实现特定条件

Java 如何在compareTo()方法中实现特定条件,java,compareto,Java,Compareto,司机: 几何对象: public static void main(String[] args) { Circle c1 = new Circle(1, "BLUE", true); Circle c2 = new Circle(2, "BLUE", true); Circle c3 = new Circle(3, "BLUE", false); Circle c4 = new Circle(4, "BLACK", true); System.out.p

司机:

几何对象:

public static void main(String[] args) {
    Circle c1 = new Circle(1, "BLUE", true);
    Circle c2 = new Circle(2, "BLUE", true);
    Circle c3 = new Circle(3, "BLUE", false);
    Circle c4 = new Circle(4, "BLACK", true);

    System.out.println("c1 ? c1 " + c1.compareTo(c1));
    System.out.println("c1 ? c2 " + c1.compareTo(c2));
    System.out.println("c1 ? c3 " + c1.compareTo(c3));
    System.out.println("c1 ? c4 " + c1.compareTo(c4));

    Rectangle r1 = new Rectangle(1, 1, "BLUE", true);
    Rectangle r2 = new Rectangle(2, 2, "RED", true);
    Rectangle r3 = new Rectangle(4, 4, "GREEN", false);
    Rectangle r4 = new Rectangle(4, 4, "GREEN", true);

    System.out.println("r1 ? r2 " + r1.compareTo(r2));
    System.out.println("r1 ? r2 " + r1.compareTo(r2));
    System.out.println("c1 ? r2 " + c1.compareTo(r2));
    System.out.println("r3 ? r4 " + r3.compareTo(r4));

}
现在我只能根据区域进行比较,我不确定如何将下面列出的其他两个标准添加到我的方法中


如果对象是圆形和蓝色,并且无论面积大小,它都比任何其他对象大,并且如果对象是透明的(填充=假),那么它只占其面积的50%,我将如何添加标准?

为了满足填充要求,我将向GeometricObject添加一个方法,如colorDependentArea()

并在compare to中使用此方法,而不是在getArea()中使用此方法


您可以使用
derivedArea=area*(填充?1.0:0.5)

如果您不想以某种特定的方式处理圆,您可以将检查添加到
compareTo
方法中:

public int compareTo(GeometricObject otherfigure) {
    if (this.colorDependentArea() == otherfigure.colorDependentArea()) {
        return 0;
    } else if (this.colorDependentArea() > otherfigure.colorDependentArea()) {
        return 1;
    }
    return -1;

}

我不确定如果对象是满足第一个条件的圆&&this.color=“blue”,语法会说什么。对于第二个标准,我会说,如果(this.filled=false)将面积除以2,占50%?说某个圆大于或小于某个矩形是什么意思?它是基于对象的面积,而不是我上面列出的两个标准。谢谢,我一直在想怎么做。simetry怎么样?下一个等式必须为真
blueCircle.compareTo(矩形)==-rectangle.compareTo(blueCircle)
。但是在您的代码
矩形中,compareTo(蓝色圆圈)
不考虑“蓝色圆圈”的数量规则。由于某种原因,当它将C1与C4进行比较时,当我使用您针对标准1的第一个解决方案时,它给出了-1而不是1。我删除了代码,因为它不正确。请参阅talex注释。当将C1与C4进行比较时,它给出了值-1而不是1,这是什么原因造成的?圆圈4是黑色的,所以它应该返回1,因为当比较c1和c4时,c1是蓝色的,因此它总是更大。但是它不起作用。我添加了这一行
else if(圆圈的其他图形实例(&&!(“blue”.equalsIgnoreCase(this.color)){return 1;
,但是它似乎仍然不起作用,它只是返回到比较区域的默认值。为什么?!(“blue”.equalsIgnoreCase(this.color)`?那么试试调试器。我不确定。我一直在更改它,但似乎没有任何效果。c1?c4应该=1,因为c1是蓝色的,c4是黑色的。
public int colorDependentArea(){
       if(!this.filled){
           return this.area/2;
       }else{ 
           return this.area;
       }

}
public int compareTo(GeometricObject otherfigure) {
    if (this.colorDependentArea() == otherfigure.colorDependentArea()) {
        return 0;
    } else if (this.colorDependentArea() > otherfigure.colorDependentArea()) {
        return 1;
    }
    return -1;

}
if(this instanceOf Circle && this.isBlue()){
     if(!(otherfigure instanceOf Circle && otherfigure.isBlue()){
         return +1;
     } else {
         // fall back to default behaviour if both object blue circles
     }
} else if (otherfigure instanceOf Circle && otherfigure.isBlue()){
     return -1;
}