问题
Let's say I have the following interface:
public interface Numeric
{
public Numeric addition(Numeric x,Numeric y);
}
And the following class:
public class Complex implements Numeric
{
private int real;
private int img;
public Complex(int real, int img){
this.real = real;
this.img = img;
}
public Numeric addition(Numeric x, Numeric y){
if (x instanceof Complex && y instanceof Complex){
Complex n1 = (Complex)x;
Complex n2 = (Complex)y;
return new Complex(n1.getReal() + n1.getReal(), n2.getImg() +
n2.getImg());
} throw new UnsupportedOperationException();
}
public int getReal(){
return real;
}
public int getImg(){
return img;
}
}
I have a couple questions:
The addition method has the return type Numeric, and it's arguments are Numeric. x and y are then verified if they are of type Complex. But how can I pass Complex arguments, when in the definition the arguments are of type Numeric? The same when I return. I return a Complex object, not Numeric. What is the correlation and logic behind this.
If x and y are Complex, because I checked in the if, why do I need to cast x and y to new 2 objects? What is the point of casting, if they are already Complex.
And why the if doesn't work without the throw? What does
UnsupportedOperationException
is, and why is it mandatory?
回答1:
As
Complex
implementsNumeric
, anyComplex
object can be used whereverNumeric
is expected.Every
Numeric
object is notComplex
. There can be another classReal
whereReal implements Numeric
. In this case, aNumeric
variable can be referencing aReal
object and you cannot substitute aReal
object withComplex
. So, you need to cast the object.As return type of method
addition
isNumeric
, your code MUST be returning an object of typeNumeric
. In case you remove throw statement and if condition is evaluated to false, method will not return anything and compiler will complain.
来源:https://stackoverflow.com/questions/59245401/java-why-can-i-pass-an-object-argument-if-in-the-method-definition-the-argumen