有个程序不明白
package cha4;
public interface QuadInterface{
public double b24ac();
public void x1x2();
}
package cha4;
public class QuadImp11 implements QuadInterface{
private double[] abc;
private double[] x1x2 = new double[2];
double rb24ac;
QuadImp11(double[] newAbc){
abc = newAbc;
rb24ac = b24ac();
}
public double b24ac(){
return abc[1] * abc[1] - 4 * abc[0] * abc[2];
}
public double x1x2(){
if(rb24ac == 0){
x1x2[0] = (-abc[1]) / (2*abc[0]);
x1x2[1] = x1x2[0];
}
else
if(rb24ac > 0){
x1x2[0] = (-abc[1] - Math.sqrt(rb24ac)) / (2 * abc[0]);
x1x2[1] = (-abc[1] + Math.sqrt(rb24ac)) / (2 * abc[0]);
}
else{
System.out.println("No real Roots!");
}
//return 1;
}
public double getX1() { return x1x2[1]; }
public double getX2() { return x1x2[0]; }
public void displayX1X2(){
if(rb24ac >= 0){
System.out.println("x1 = "+x1x2[0]);
System.out.println("x2 = "+x1x2[1]);
}
}
}
这两个程序书上的要求是接口QuadInterface的第一个实现类QuadImp11
都打完了,第二个程序结果是
--------------------Configuration: <Default>--------------------
E:\course\Java\第四章\QuadImp11.java:2: cha4.QuadImp11 不是抽象的,并且未覆盖 cha4.QuadInterface 中的抽象方法 x1x2()
public class QuadImp11 implements QuadInterface{
^
E:\course\Java\第四章\QuadImp11.java:15: cha4.QuadImp11 中的 x1x2() 无法实现 cha4.QuadInterface 中的 x1x2();正在尝试使用不兼容的返回类型
找到: double
需要: void
public double x1x2(){
^
2 错误
Process completed.
请各位帮忙看下,谢谢了
------解决方案--------------------Java code
public interface QuadInterface
{
public void x1x2();//返回类型是void,而你在下面实现时,变成了double类型,这不是重写
}
public class QuadImp11 implements QuadInterface
{
public double x1x2(){}
}
------解决方案--------------------
Java code
//自己再添加一个public class, 包含main 方法
interface QuadInterface {
public double b24ac();
public void x1x2();
}
class QuadImp11 implements QuadInterface {
private double[] abc;
private double[] x1x2 = new double[2];
double rb24ac;
QuadImp11(double[] newAbc) {
abc = newAbc;
rb24ac = b24ac();
}
public double b24ac() {
return abc[1] * abc[1] - 4 * abc[0] * abc[2];
}
public void x1x2() {
if (rb24ac == 0) {
x1x2[0] = (-abc[1]) / (2 * abc[0]);
x1x2[1] = x1x2[0];
} else if (rb24ac > 0) {
x1x2[0] = (-abc[1] - Math.sqrt(rb24ac)) / (2 * abc[0]);
x1x2[1] = (-abc[1] + Math.sqrt(rb24ac)) / (2 * abc[0]);
}
else {
System.out.println("No real Roots!");
}
// return 1;
}
public double getX1() {
return x1x2[1];
}
public double getX2() {
return x1x2[0];
}
public void displayX1X2() {
if (rb24ac >= 0) {
System.out.println("x1 = " + x1x2[0]);
System.out.println("x2 = " + x1x2[1]);
}
}
}
------解决方案--------------------
Java code
你的代码