public class This_testing {
int x,y;
public This_testing(int x,int y){ //Why modifier void here would cause exception?
this.x=x;
this.y=y;
}
public static void main(String [] args){
This_testing t =new This_testing(40,50);
System.out.println(t.x+" "+t.y);
}
}
为什么对构造函数使用修饰符void This_testing会导致以下异常:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -
constructor This_testing in class practice.This_testing cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
at practice.This_testing.main(This_testing.java:13)
答案 0 :(得分:6)
构造函数不返回任何内容,甚至不返回void
。它只是如何在语言规范中定义它们。
您可以创建一个名为This_testing
的方法,它返回void
,但这将被视为方法,而不是构造函数(因此,使用new This_testing(x, y)
将无法编译)。