Java。为什么我不能将接口对象转换为 class 对象?
Java. Why I can't convert a interface object to a class object?
我有这个界面:
public interface Numeric {
public Numeric addition(Numeric x,Numeric y);
public Numeric subtraction(Numeric x,Numeric y);
}
还有这个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 Numeric subtraction(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;
}
}
为什么会出现此错误:
incompatible types: Numeric cannot be converted to Complex
当我运行这个代码时:
public class TestNumeric {
public static void main(String[] args) {
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, 2);
Complex rez;
rez = rez.addition(c1, c2);
}
}
错误在行 "rez = rez.addition(c1, c2);"
Complex 实现了 Numeric,所以每个 Numeric 都是 Complex,对吧?我已经在添加方法中完成了转换和检查。为什么会出现此错误,我该如何解决?
addition
和subtraction
的声明应该如下:
public interface Numeric {
public Numeric addition(Numeric obj);
public Numeric subtraction(Numeric obj);
}
和addition
和subtraction
的执行应该如下:
public Numeric addition(Numeric obj){
if (obj instanceof Complex){
Complex n = (Complex)obj;
return new Complex(this.getReal() + n.getReal(), this.getImg() +
n.getImg());
} else {
throw new UnsupportedOperationException();
}
}
最后,TestNumeric
应该是这样的:
public class TestNumeric {
public static void main(String[] args) {
Numeric c1 = new Complex(3, 4);
Numeric c2 = new Complex(1, 2);
Numeric rez = c1.addition(c2);
}
}
[更新]
@OrosTom - 根据您的评论,我在下面添加了您需要放在 class、Complex
中的方法,以便您可以打印结果
@Override
public String toString() {
return real + " + " + img + "i";
}
注意:请查看 https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html(部分,toString() 方法)以获取更多详细信息。
在此之后,输出以下代码
public class TestNumeric {
public static void main(String[] args) {
Numeric c1 = new Complex(3, 4);
Numeric c2 = new Complex(1, 2);
Numeric rez = c1.addition(c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(rez);
}
}
将是:
3 + 4i
1 + 2i
4 + 6i
我有这个界面:
public interface Numeric {
public Numeric addition(Numeric x,Numeric y);
public Numeric subtraction(Numeric x,Numeric y);
}
还有这个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 Numeric subtraction(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;
}
}
为什么会出现此错误:
incompatible types: Numeric cannot be converted to Complex
当我运行这个代码时:
public class TestNumeric {
public static void main(String[] args) {
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, 2);
Complex rez;
rez = rez.addition(c1, c2);
}
}
错误在行 "rez = rez.addition(c1, c2);"
Complex 实现了 Numeric,所以每个 Numeric 都是 Complex,对吧?我已经在添加方法中完成了转换和检查。为什么会出现此错误,我该如何解决?
addition
和subtraction
的声明应该如下:
public interface Numeric {
public Numeric addition(Numeric obj);
public Numeric subtraction(Numeric obj);
}
和addition
和subtraction
的执行应该如下:
public Numeric addition(Numeric obj){
if (obj instanceof Complex){
Complex n = (Complex)obj;
return new Complex(this.getReal() + n.getReal(), this.getImg() +
n.getImg());
} else {
throw new UnsupportedOperationException();
}
}
最后,TestNumeric
应该是这样的:
public class TestNumeric {
public static void main(String[] args) {
Numeric c1 = new Complex(3, 4);
Numeric c2 = new Complex(1, 2);
Numeric rez = c1.addition(c2);
}
}
[更新]
@OrosTom - 根据您的评论,我在下面添加了您需要放在 class、Complex
中的方法,以便您可以打印结果
@Override
public String toString() {
return real + " + " + img + "i";
}
注意:请查看 https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html(部分,toString() 方法)以获取更多详细信息。
在此之后,输出以下代码
public class TestNumeric {
public static void main(String[] args) {
Numeric c1 = new Complex(3, 4);
Numeric c2 = new Complex(1, 2);
Numeric rez = c1.addition(c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(rez);
}
}
将是:
3 + 4i
1 + 2i
4 + 6i