2016-08-05 22 views
-2
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教程中创建了上述程序。问题是我无法计算具有双数据类型的盒子的音量。运行时重载的方法解析以及类型提升如何工作?

程序结果显示如下:

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

其次,我感到困惑的方法重载的Java的概念,因为我知道,在方法重载,我们可以使用相同的方法名与不同的参数,但如我创建了卷方法,我不得不修改方法的名称(volume,vol),以便重载卷方法并获得答案。这是为什么?

+0

重载'volume()' - *因为我知道在方法重载时,我们可以使用相同的方法名称与不同的参数* - 关键词是参数,您不能通过更改它们的返回来重载方法类型。 –

+0

原因很明显:您将整数传递给构造函数,所以double值从不设置。他们总是零。 – duffymo

回答

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因此输出你get是0.

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

方法1:

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); 
    } 

} 

输出:你好诠释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); 
    } 

} 

输出:你好长10

原因:由于存在用于值10(其是int类型的)不匹配的方法,它是晋升为long。现在有一个接受long参数的方法,它被调用。

+0

为什么投?你可以使用十进制数字 –

+0

是的,那是另一种方法。我正在演示如何调用适当的方法。按照您提出的上述建议更新了答案 – JavaHopper

+0

,是否需要明确声明为25.0或25d?为什么自动型促销概念不适用于此?我的意思是说,当我打电话双卷方法为什么不自动获得提升双倍 –

0

指定它是这样的双重价值:

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 
1

我认为有两组属性是错误的。以下是我如何写它。

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; 
    } 

} 

您需要设置所有的变量。

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

+0

是@Vijay Kannan ...建议的方式是从构造函数中分配值而不是'Dimension'方法 –

相关问题