如果这个问题主要是基于意见的,请原谅我,但我觉得它不是,并且有充分的理由选择。所以,这里有一个例子。对不起,它真的很长,但超级简单:
界面:
public interface Shape
{
double area ();
}
实现类 1:
import static java.lang.Math.PI;
public class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double area()
{
return PI*radius*radius;
}
}
实施类 2:
public class Square implements Shape
{
private double size;
public Square(double sideLength)
{
size = sideLength;
}
public double area()
{
return size*size;
}
}
司机:
Shape[] shapes = new Shape[]{new Circle (5.3), new Square (2.4)};
System.out.println(shapes[0].area()); //prints 88.247...
System.out.println(shapes[1].area()); //prints 5.76
这有效,因为.area()
它被Circle
and覆盖Square
。现在,这是我的问题真正开始的地方。假设驱动程序有这些方法:
public static void whatIs(Shape s)
{
System.out.println("Shape");
}
public static void whatIs(Circle s)
{
System.out.println("Circle");
}
public static void whatIs(Square s)
{
System.out.println("Square");
}
如果我们调用:
whatIs(shapes[0]); //prints "Shape"
whatIs(shapes[1]); //prints "Shape"
发生这种情况是因为 Java 将对象解释为Shape
s 而不是Circle
and Square
。当然我们可以通过以下方式得到想要的结果:
if (shapes[0] instanceof Circle)
{
whatIs((Circle) shapes[0]); //prints "Circle"
}
if (shapes[1] instanceof Square)
{
whatIs((Square) shapes[1]); //prints "Square"
}
既然我们有了背景,我的问题是:
什么原因导致编译器/语言设计whatIs(shapes[0]);
会打印“形状”?例如,为什么 Java 编译器可以准确区分相关对象的重载方法,但不能区分重载方法?更具体地说,如果驱动程序可以访问的唯一方法是:
public static void whatIs(Circle s)
{
System.out.println("Circle");
}
public static void whatIs(Square s)
{
System.out.println("Square");
}
我们试图打电话,
whatIs(shapes[0]);
whatIs(shapes[1]);
我们将得到两个错误(一个 forSquare
和一个 for Circle
),表明:
- 方法 Driver.whatIs(Square) 不适用
- 实参Shape无法通过方法调用转换为Square
那么,既然我们已经了解了细节,为什么 Java 不能处理这样的情况呢?例如,这样做是出于效率问题吗,是否由于某些设计决策而无法实现,出于某种原因,这是一种不好的做法,等等?