我在博士Java中收到一条错误消息,表示我的构造函数未定义为String,int,int,尽管我的构造函数具有这些参数(以相同顺序),并且所有内容都是大小写匹配的。这也不是一个课程像另一个线程建议的那样过时的问题。为什么Java说我的构造函数是未定义的,即使它是?
这里是我的“商城”类构造函数接受一个字符串,一个int和一个int
public class Mall{
//declare variables
private String name;//name of the mall
private int length; //length of the mall = # of columns of stores array
private int width; //width of the mall = # of rows of stores array
public void Mall(String name, int length, int width){
//this is the constructor I want to use
this.name=name;
this.length=length;
this.width=width;
}
}
,这里是我的主要方法
public class Test1{
public static void main(String[] args){
Mall m = new Mall("nameOfMall", 3, 3); //here is where the error happens
}
}
我试图创建一个构造函数没有参数,然后在我的对象创建语句中传递没有参数,虽然这不会导致任何编译错误,但它不会将它设置为适当的值。此外,我可以调用Mall类中的其他方法,这使得我相信这是创建语句的问题,而不是Mall类中的任何问题。我有权这样想吗?什么导致了错误?
我没有看到任何构造函数。 (提示:'void'。投票结束为拼写错误。) –
void这个词让Java将以下内容理解为方法,而不是构造函数。构造函数根本没有返回类型。 – RealSkeptic
我不知道,谢谢! – JessStormBorn