【问题标题】:How can I get the return value of a superclass method without executing it?如何在不执行超类方法的情况下获取它的返回值?
【发布时间】:2015-02-09 03:50:26
【问题描述】:

这是我的超类 equals() 方法:

public boolean equals(Object other){
    Car c = (Car)other;
    if(this.make.equals(c.make) && this.model.equals(c.model)){

        System.out.println("True, Cars are equal");
        return true;
    }

    else 
        System.out.println("False, Cars are not equal");
    return false;

}

这是我的子类 equals() 方法:

public boolean equals(Object other) {
    GreenCar g = (GreenCar) other;
    if(super.equals(g)==true){

        if (this.type.equals(g.type)) {
            System.out.println("True, Cars are equal");
            return true;
        } else {
            System.out.println("False, Cars are not equal");

            return false;
        }
    }
    else
        System.out.println("False, Cars are not equal");
    return false;
}

当它在if(super.equals(g)==true){ 运行检查时,它会执行该方法并打印出 true 或 false。我如何才能检查返回值?

【问题讨论】:

  • 请将晚餐改为超级。此外,如果您希望 equals() 方法不打印任何内容,只需删除打印语句即可。
  • 在您的 super 类中删除对 System.out.println 的调用。

标签: java equals hierarchy


【解决方案1】:

如果不打印任何内容,您将无法运行该方法。

这就是为什么你的大多数方法不应该有“副作用”(比如将东西打印到屏幕上)。

从两个equals 方法中删除println 调用。将它们添加到调用 equals 的代码中。

【讨论】:

  • 把它加到调用equals的代码中是什么意思?像我的主要方法一样?
  • @LesterRamos:你在某处写了something.equals(somethingElse) 对吗?你想让它打印它们是否相等?
  • 是的,本来我想这样调用方法:c1.equals(c2);但现在我必须将它包含在 System.out.println() 中,如果我去掉该方法的“副作用”;所以它只会打印出非常简单的“true”或“false”。
  • @LesterRamos if(c1.equals(c2)) System.out.println("They are equal"); else System.out.println("They aren't equal");
  • 或者(如果你知道?:)你可以把它缩写成System.out.println(c1.equals(c2) ? "They are equal" : "They aren't equal");
【解决方案2】:

在你的 super 类中,你可以写类似

protected boolean equals(Object other, boolean debug) {
    if (other instanceof Car) {
        Car c = (Car) other;
        if (this.make.equals(c.make) && this.model.equals(c.model)) {
            if (debug) {
                System.out.println("True, Cars are equal");
            }
            return true;
        }
    }
    if (debug) {
        System.out.println("False, Cars are not equal");
    }
    return false;
}

然后您可以修改您的 equals() 方法(仍在 super 类中),例如

public boolean equals(Object other) {
    return equals(other, true); // <-- default to debug.
}

接下来你的subclass 应该调用带有debug 标志的版本

if (super.equals(g, false)) { // you don't need == true

或者,您可以使用Logger 并按需启用和禁用debug

【讨论】:

    【解决方案3】:

    就像人们说的那样,如果您希望它在调用时不打印某些内容,则需要取出那些 println,因为每次调用该方法时它都会打印。

    就像注释一样,您可以通过删除其他方法来缩短某些方法,因为如果条件为真,它会在进入下一个代码体之前返回。例如

    public boolean equals(Object other){
        Car c = (Car)other;
        if(this.make.equals(c.make) && this.model.equals(c.model))//if this is true
            return true;//the method ends here
        return false;//if the method hasn't ended yet then the conditional must be false
     }
    

    我还注意到您使用了if(super.equals(g)==true),但如果您只输入if(super.equals(g)),它具有相同的效果,因为您输入了一个布尔值并检查该布尔值是否为真。如果您想获得if((boolean)==false) 的效果,您可以使用if(!(boolean)),因为它会检查与该布尔值相反的值是否为真。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 2021-05-28
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多