【问题标题】:Overloaded method resolution at runtime and how type promotion works?运行时重载的方法解析以及类型提升如何工作?
【发布时间】:2016-12-11 09:22:15
【问题描述】:
package arunjava;

public class sample3 {

    public static void main(String[] args) {
        Box25 b1 = new Box25();
        Box25 b2 = new Box25();


        b1.Dimension(25, 32, 65);
        b2.Dimension(25, 45, 62);

        System.out.println("volume is" + b1.volume());
        System.out.println("volume is" + b2.volume());

        b1.Dimension(4, 6, 8);
        b2.Dimension(6, 8, 4);

        System.out.println("volume is" + b1.vol());
        System.out.println("volume is" + b2.vol());
    }
}

class Box25 {
    double height, width, depth;

    int height1, width1, depth1;

    public void Dimension(double height, double width, double depth) {
        this.height = height;
        this.width = width;
        this.depth = depth;
    }

    public void Dimension(int height1, int width1, int depth1) {
        this.height1 = height1;
        this.width1 = width1;
        this.depth1 = depth1;
    }

    double volume() {
        return height * depth * width;
    }

    int vol() {
        return height1 * depth1 * width1;
    }
}

我在我的 Java 教程中创建了上述程序。问题是我无法计算具有 double 数据类型的盒子的体积。

程序结果显示如下:

volume is0.0
volume is0.0
volume is192
volume is192

其次,我对 Java 方法重载的概念感到困惑,因为我知道在方法重载中我们可以使用具有不同参数的相同方法名称,但是在创建卷方法时,我不得不修改方法的名称(volume,vol),从而重载volume方法并得到答案。为什么会这样?

【问题讨论】:

  • 关于重载volume() -- 我知道在方法重载中我们可以使用相同的方法名和不同的参数 -- 关键字是参数,你可以'不要通过改变返回类型来重载方法。
  • 原因很明显:您将整数传递给构造函数,因此永远不会设置双精度值。它们始终为零。

标签: java methods int double overloading


【解决方案1】:

以下几行调用

b1.Dimension(25, 32, 65);
b1.Dimension(4, 6, 8);

调用方法 public void Dimension(int height1, int width1, int depth1) 因为字面量的 25、32 和 65 被处理为 int 类型

因此,Box25 类的字段 height, width, depth 具有默认值 0.0,因此您得到的输出为 0。

有多种方法可以解决问题,通过调用适当的方法

方法一:

b1.Dimension((double)25, (double)32, (double)65);
b1.Dimension((double)4, (double)6, (double)8);

方法2:

double h = 25, w = 32, d = 65;
b1.Dimension(25, 32, 65);

方法 3:

b1.Dimension(25.0, 32.0, 65.0);
b1.Dimension(4.0, 6.0, 8.0);

为什么 int 参数被传递,而不是提升为 double?

  • 在运行时,首先通过参数类型的精确匹配进行解析
  • 如果找到匹配类型,则调用具有完全匹配参数的方法
  • 如果参数不完全匹配,则将类型提升为下一个更高的类型,如果匹配,则调用该方法。

示例 1:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

输出:你好双10.0

原因:由于值 10(int 类型)没有匹配方法,因此将其提升为 long。由于没有接受long 参数的匹配方法,它被提升为double。现在有了一个接受double 参数的方法,就会调用它。

示例 2:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(int x) {
        System.out.println("Hello int  " + x);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

输出: Hello int 10

原因:由于存在值 10(int 类型)的匹配方法,因此被调用。

示例 3:

public class OverloadingDemo {

    public static void main(String args[]) {
        OverloadingDemo obj = new OverloadingDemo();
        obj.sayHello(10);
    }

    public void sayHello(long x) {
        System.out.println("Hello long  " + x);
    }

    public void sayHello(double x) {
        System.out.println("Hello double  " + x);
    }

}

输出:Hello long 10

原因:由于值10(int类型)没有匹配方法,所以提升为long。现在有了一个接受long 参数的方法,它就会被调用。

【讨论】:

  • 为什么要投?你可以使用十进制数
  • 是的,那是另一种方法。我正在演示如何调用适当的方法。更新了答案
  • 正如您建议的上述方法,是否有必要明确声明为 25.0 或 25d ?为什么自动类型提升概念在这里不起作用?我的意思是说,当我调用双倍音量方法时,为什么参数不会自动提升为双倍
  • @VijayKannan,好问题!我已经用更多示例更新了答案,这可以帮助您了解运行时重载方法的解析
【解决方案2】:

我认为拥有两组属性是错误的。我是这样写的。

public class Box {

   private final double l, h, w;

   public Box(double length, double height, double width) {
      this.l = length; 
      this.h = height;
      this.w = width;
   }

   public double getVolume() { return this.l*this.h*this.w; }
}

正如所写,没有什么可以阻止您输入负维度或零维度。那有意义吗?你应该怎么做才能保证合理的价值观?

如果你坚持要继续上课,以下是解决问题的方法:

class Box25
{
    double height,width,depth;

    int height1,width1,depth1;

    public void Dimension(double height, double width, double depth)
    {
        this.height=height;
        this.width=width;
        this.depth=depth;

        this.height1 = (int) height;
        this.width1 = (int) width;
        this.depth1= (int) depth;

    }

    public void Dimension(int height1, int width1, int depth1)
    {
        this.height1=height1;
        this.width1=width1;
        this.depth1=depth1;

        this.height = height1;
        this.width = width1;
        this.depth = depth1;    
    }

      double volume()
    {
        return height*depth*width;
    }
     int vol()
    {
        return height1*depth1*width1;
    }

}

您需要设置所有变量。

现在这两种方法都会返回你想要的。我仍然认为你的方式是不必要的。

【讨论】:

  • 是的@Vijay Kannan ...建议的方法是像这样从构造函数分配值而不是Dimension方法
【解决方案3】:

指定它是这样的双值:

public static void main(String[] args) 
    {
        Box25 b1=new Box25();
        Box25 b2=new Box25();


        b1.Dimension(25d, 32d, 65d);
        b2.Dimension(25d, 45d, 62d);
        ...
        ...
}

输出

volume is52000.0
volume is69750.0
volume is192
volume is192

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多