为什么Java说我的构造函数是未定义的,尽管它是未定义的?
我在Dr.Java中收到一条错误消息,说我的构造函数没有为String,int,int定义,尽管我的构造函数有这些参数(顺序相同),而且所有内容都是大小写匹配的。这也不是一个类像另一个线程建议的那样保存过期的问题 这是我的“Mall”类,构造函数接受一个字符串int和一个int为什么Java说我的构造函数是未定义的,尽管它是未定义的?,java,constructor,Java,Constructor,我在Dr.Java中收到一条错误消息,说我的构造函数没有为String,int,int定义,尽管我的构造函数有这些参数(顺序相同),而且所有内容都是大小写匹配的。这也不是一个类像另一个线程建议的那样保存过期的问题 这是我的“Mall”类,构造函数接受一个字符串int和一个int public class Mall{ //declare variables private String name;//name of the mall private int length; //leng
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
这是一种方法:
public void Mall(String name, int length, int width){
this.length=length;
this.width=width;
}
这是一个构造函数:
public Mall(String name, int length, int width)
{
this.length = length;
this.width = width;
}
从构造函数中删除返回类型
void
关于构造函数的更多详细信息是:删除
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;
}
我没有看到任何构造函数。(提示:
void
。投票以键入方式关闭。)void
一词使Java将以下内容理解为一种方法,而不是构造函数。构造函数根本没有返回类型。我不知道,谢谢!你所拥有的是一个方法,而不是一个构造函数。public-void(…
告诉Java这是一个不返回值的方法,因此是void
。如果您想要一个构造函数,请使用public-my构造函数(…
。该链接不是到JLS的。我做了更正。