【问题标题】:Return in the method overloading在方法重载中返回
【发布时间】:2016-11-12 01:00:10
【问题描述】:

我是 Java 新手,我正在自己学习它。我在尝试方法重载时遇到了麻烦。这是代码

 public static void main(String[] args) {
        calculateScore();
        calculateScore(500);
        calculateScore("Duy", 600);
        calcFeetAndInchesToCentimetres(100, 3.5);
        calcFeetAndInchesToCentimetres(100*12 + 3.5);
    }

 public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
        if (feet >= 0 && inches >= 0 && inches <= 12) {
            double footToInches = feet * 12;
            double centimetres = (inches + footToInches) * 2.54;
            System.out.println("The value in centimetres is " + centimetres + " cm.");
            return centimetres;
        } else {
            return -1;
        }
    }

    public static double calcFeetAndInchesToCentimetres(double inches) {
        if (inches >= 0){
            double inchesToFeet = inches / 12;
            double inchesRemain = inches - (inchesToFeet * 12);
            calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
            return 0;
        } else {
            return -1;
        }

我的问题是当我从第二种方法中获取return 0 时,调试器说“缺少返回语句”。然后我尝试输入return calcFeetAndInchesToCentimetres(inches);,它可以工作但程序运行了大约数千次。

然后我输入return 0,一切正常。但是我不明白为什么我不能把return calcFeetAndInchesToCentimetres(inches); 放在上面,当上面的方法(带有2个参数)已经有了它时,为什么我需要一个return语句。如果我想在执行第二种方法(仅使用“英寸”参数)时转换厘米的值,我该怎么办?

我在这个块代码中意识到的另一件事

double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);

inchesRemain 将为 0?但是这个方法效果很好。当我更改inchesToFeet = inches % 12 时,它只是没有显示任何内容。为什么?

【问题讨论】:

    标签: java methods return overloading


    【解决方案1】:

    应该是:

    public static double calcFeetAndInchesToCentimetres(double inches) {
        if (inches >= 0){
            double inchesToFeet = inches / 12;
            double inchesRemain = inches - (inchesToFeet * 12);
            return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
        } else {
            return -1;
        }
    }
    

    你说你已经尝试过return calcFeetAndInchesToCentimetres(inches);,但这只是递归调用你的方法,它会永远递归,因为没有停止条件。

    【讨论】:

    • 返回是把值放到方法里吧?我的意思是,如果我不返回,那会发生什么?因为我只在调用后需要使用方法内声明的变量时才放return
    • 哇,这对回报是一个很大的误解。抛开void 方法不谈,return 是方法中最重要的东西,当然不是用于访问方法内部的变量。函数的真正目的是获取参数列表(可能为空)并根据它们返回一个值。如果没有返回语句,您只能编写void 方法,这些方法 的东西而不是 计算 的东西。您应该真正阅读有关函数/方法的内容,因为您不能承受对这些概念的任何误解
    • 你能详细解释一下吗?你能给我推荐一本关于java编程的书吗?我是越南人,我搜索的所有网络都说当我们需要保留信息并返回信息的方法时使用“返回”。而且我不明白为什么'return'会再次调用该方法(在我的代码中)?
    • return 不会再次调用您的方法,您在编写calcFeetAndInchesToCentimetres(inches) 时正在调用您的方法。你做过数学吗?如果你这样做了,那么你就知道函数是什么了。在编程中没有什么不同,一个函数接受参数并返回一个值。这就是return的唯一含义
    • 谢谢。我得到了它。我想问的最后一件事是我问题中的编辑部分。你看到了吗?我认为这是一个错误@@
    【解决方案2】:

    通过方法重载,您有两种不同的方法。

    1. calcFeetAndInchesToCentimetres 接受一个参数
    2. calcFeetAndInchesToCentimetres 接受两个参数

    现在,当您调用 calcFeetAndInchesToCentimetres(inches); 时,您正在调用带有单个参数的那个。如果您从内部调用它,它将继续无限次调用自己。这是您看到的错误。

    如果您将其替换为 return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain); 将调用另一种方法 - 采用两个参数的方法。这才是你真正想做的。

    固定版本:

    public static double calcFeetAndInchesToCentimetres(double inches) {
        if (inches >= 0){
            double inchesToFeet = inches / 12;
            double inchesRemain = inches - (inchesToFeet * 12);
            return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
        } else {
            return -1;
        }
    }
    

    【讨论】:

      【解决方案3】:
      public static void main(String[] args) {
          calculateScore();
          calculateScore(500);
          calculateScore("Duy", 600);
          calcFeetAndInchesToCentimetres(100, 3.5);
          calcFeetAndInchesToCentimetres(100*12 + 3.5);
      }
      
      public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
          if (feet >= 0 && inches >= 0 && inches <= 12) {
              double footToInches = feet * 12;
              double centimetres = (inches + footToInches) * 2.54;
              System.out.println("The value in centimetres is " + centimetres + " cm.");
              return centimetres;
          } else {
              return -1;
          }
      }
      public static double calcFeetAndInchesToCentimetres(double inches) {
          if (inches >= 0){
              double inchesToFeet = inches / 12;
              double inchesRemain = inches - (inchesToFeet * 12);
              calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
              return 0; //Here
          } else {
              return -1;
          }
      

      如果您删除 return 0 它会显示缺少 return 语句,因为您处于 if-else 循环中。 假设您的输入英寸小于 0,那么它将进入 else 部分并返回 -1 .. 但是如果输入的英寸大于 0,那么它将进入 if 条件以及何时到达 if 的末尾语句,那么就没有任何东西可以返回,所以对于 if-else 条件,if 和 else 都应该返回一些东西。

      解决这个问题的其他方法是从 if-else 条件中创建一个局部变量,并在 else 部分完成后返回 if 。这样,天气它会进入 if 或 else 部分的代码,两次都会有要返回的局部变量的一些值..

      现在是您问题的第二部分: 你的代码看起来像

      line 1   public static double calcFeetAndInchesToCentimetres(double inches) {
      

      第2行if (inches >= 0){ 第3行double inchesToFeet = inches / 12; 第4行double inchesRemain = inches - (inchesToFeet * 12); 第5行calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain); 第6行return calcFeetAndInchesToCentimetres(inches); } else { return -1; }

      所以在这种情况下,您一次又一次地调用相同的方法本身.. 您从第 1 行开始,一直到第 6 行,然后调用相同的方法并再次开始执行,第 1 行将执行到第 6 行,然后一遍又一遍地执行第 1 行,直到所有内存丢失并发生堆栈溢出。.

      最佳实践代码如下所示:

      public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
          double centimetres = 0.0;
          if (feet >= 0 && inches >= 0 && inches <= 12) {
              double footToInches = feet * 12;
              centimetres = (inches + footToInches) * 2.54;
              System.out.println("The value in centimetres is " + centimetres + " cm.");
          } else {
              centimetres = -1;
          }
          return centimetres;
      }
      
      
      public static double calcFeetAndInchesToCentimetres(double inches) {
          double centimeters = 0;
          if (inches >= 0) {
              double inchesToFeet = inches / 12;
              double inchesRemain = inches - (inchesToFeet * 12);
              centimeters = calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
      
          } else {
              centimeters = -1;
          }
          return centimeters; //Here
      }
      

      【讨论】:

      • 我不认为单个 return 语句比多个 return 语句更好。实际上,在大多数情况下,我对前者有强烈的偏好。实际上,最终变量使代码比多次分配的变量更清晰。
      • 这意味着我不必在每个 if 和 else 块代码中都输入一个 return?
      • @Dici 这取决于个人的编码实践.. 我强烈遵循一个返回语句.. 代码很长的情况有很多,每当我们有多个返回语句时,有时我们会忘记这一点并且当我们执行时,我们认为我们哪里出错了。为什么它没有返回所需的输出。
      • @DuyNghĩaTrần 不,您不必在每个块中都返回 return 。只要确保您在方法本身的右大括号之前返回一些东西。
      • 我认为一个方法首先不应该很长:p
      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2016-02-09
      相关资源
      最近更新 更多