2015-04-12 56 views
-2

我在博士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类中的任何问题。我有权这样想吗?什么导致了错误?

+8

我没有看到任何构造函数。 (提示:'void'。投票结束为拼写错误。) –

+3

void这个词让Java将以下内容理解为方法,而不是构造函数。构造函数根本没有返回类型。 – RealSkeptic

+0

我不知道,谢谢! – JessStormBorn

回答

4

你有一个方法,而不是一个构造函数。一个构造函数没有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; 
} 
2

从构造函数中删除返回类型void

构造上更详细的信息是:Here

+1

这个链接是*不*给JLS。 – hexafraction

+0

我做了更正。 – Nirmal

0

删除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; 
    } 
相关问题