如果我尝试将参数放在下一行,则会收到错误消息
Sum s1 = new Sum(1,5,6);
为什么有3个参数的Sum构造函数时会出现此错误
package test;
class Sum {
double num1,num2,num3;
double[] result = new double[4];
double[] Sum(int num1,int num2, int num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
result[0] = num1;
result[1] = num2;
result[2] = num3;
result[4] = num1+ num2 + num3;
return result;
}
}
public class Test {
public static void main(String[] args) {
Sum s1 = new Sum(1,5,6);
}
}
最佳答案
您的构造函数具有返回类型,构造函数没有return语句。你需要改变
public Sum(int num1,int num2, int num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
result[0] = num1;
result[1] = num2;
result[2] = num3;
result[3] = num1+ num2 + num3;
}