运行时的重载方法解析以及类型提升如何工作?
Overloaded method resolution at runtime and how type promotion works?
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 方法时,我不得不修改方法名(volume,vol),重载volume方法得到答案。为什么会这样?
以下行调用
b1.Dimension(25, 32, 65);
b1.Dimension(4, 6, 8);
调用方法 public void Dimension(int height1, int width1, int depth1)
因为文字的 25、32 和 65 被视为类型 int
因此,class Box25
的字段 height, width, depth
具有默认值 0.0,因此您获得的输出为 0。
有多种方法可以解决问题,方法是调用适当的方法
方法一:
b1.Dimension((double)25, (double)32, (double)65);
b1.Dimension((double)4, (double)6, (double)8);
方法二:
double h = 25, w = 32, d = 65;
b1.Dimension(25, 32, 65);
方法三:
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
参数,它被调用。
例二:
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);
}
}
输出:你好长10
原因:由于值10(int
类型)没有匹配方法,因此提升为long
。现在有一个方法接受 long
参数,它被调用。
像这样指定它是双精度值:
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
我认为拥有两组属性是错误的。我是这样写的。
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,请按以下方法解决问题:
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;
}
}
您需要设置所有变量。
现在这两种方法都会 return 您想要的。我仍然认为你的方式是不必要的。
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 方法时,我不得不修改方法名(volume,vol),重载volume方法得到答案。为什么会这样?
以下行调用
b1.Dimension(25, 32, 65);
b1.Dimension(4, 6, 8);
调用方法 public void Dimension(int height1, int width1, int depth1)
因为文字的 25、32 和 65 被视为类型 int
因此,class Box25
的字段 height, width, depth
具有默认值 0.0,因此您获得的输出为 0。
有多种方法可以解决问题,方法是调用适当的方法
方法一:
b1.Dimension((double)25, (double)32, (double)65);
b1.Dimension((double)4, (double)6, (double)8);
方法二:
double h = 25, w = 32, d = 65;
b1.Dimension(25, 32, 65);
方法三:
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
参数,它被调用。
例二:
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);
}
}
输出:你好长10
原因:由于值10(int
类型)没有匹配方法,因此提升为long
。现在有一个方法接受 long
参数,它被调用。
像这样指定它是双精度值:
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
我认为拥有两组属性是错误的。我是这样写的。
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,请按以下方法解决问题:
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;
}
}
您需要设置所有变量。
现在这两种方法都会 return 您想要的。我仍然认为你的方式是不必要的。