构造函数ClassCountry(String,String,String,int)未定义

时间:2018-09-25 06:22:29

标签: java string

Eclipse一直说构造函数是未定义的-这是怎么回事?我已经检查了一切。

package exerciseOne;

public class TestCountryClass {

    public static void main(String[] args) {

        ClassCountry oCon1 = new ClassCountry("Iceland", "Icelandic", "króna", 400000);         

        System.out.printf("%s %s %d%n", oCon1.getCountryName(),
                                        oCon1.getCountryLanguage(),
                                        oCon1.getCountryCurrency(),
                                        oCon1.getCountryPopulation());

    }

}

这是该类的代码,我已经检查了多次,但是Eclipse一直返回相同的错误消息。我希望你们能在这里找到问题;任何帮助表示赞赏:

package exerciseOne;

public class ClassCountry {

    private String countryName;
    private String countryLanguage;
    private String countryCurrency;
    private int countryPopulation;

    public void classCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
    {
        this.countryName = countryName;
        this.countryLanguage = countryLanguage;
        this.countryCurrency = countryCurrency;
        this.countryPopulation = countryPopulation;

    }

    public void setCountryName(String countryName)
    {
        this.countryName = countryName;
    }
    public String getCountryName()
    {
        return countryName;
    }
    public void setCountryLanguage(String countryLanguage)
    {
        this.countryLanguage = countryLanguage;
    }
    public String getCountryLanguage()
    {
        return countryLanguage;
    }
    public void setCountryPopulation(int countryPopulation)
    {
        this.countryPopulation = countryPopulation;
    }
    public int getCountryPopulation()
    {
        return countryPopulation;
    }

    public void setCountryCurrency(String countryCurrency)
    {
        this.countryCurrency = countryCurrency;
    }
    public String getCountryCurrency()
    {
        return countryCurrency;
    }

}

6 个答案:

答案 0 :(得分:3)

您的“构造函数”不是构造函数,因为它是一种方法。

classCountry更改为ClassCountry并删除空白。

public ClassCountry(...

答案 1 :(得分:2)

public void classCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
{
    this.countryName = countryName;
    this.countryLanguage = countryLanguage;
    this.countryCurrency = countryCurrency;
    this.countryPopulation = countryPopulation;

}

这是您的问题。您已将其添加为构造函数,但这不是构造函数。这是一个返回类型为void的方法。

将以上内容更改为:

public ClassCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation)
{
    this.countryName = countryName;
    this.countryLanguage = countryLanguage;
    this.countryCurrency = countryCurrency;
    this.countryPopulation = countryPopulation;

}

有关构造函数的更多信息,可以检查此Oracle tutorial

答案 2 :(得分:0)

package com.String;

public class StackOverFlow {

public static void main(String[] args) {

    ClassCountry oCon1 = new ClassCountry("Iceland", "Icelandic", "króna", 400000);         

    System.out.printf("%s %s %s %d", oCon1.getCountryName(),
                                    oCon1.getCountryLanguage(),
                                    oCon1.getCountryCurrency(),
                                    oCon1.getCountryPopulation());

}



private static class ClassCountry{

    private String countryName;
    private String countryLanguage;
    private String countryCurrency;
    private int countryPopulation;

    public ClassCountry(String countryName, String countryLanguage, String countryCurrency, int countryPopulation) {
        super();
        this.countryName = countryName;
        this.countryLanguage = countryLanguage;
        this.countryCurrency = countryCurrency;
        this.countryPopulation = countryPopulation;
    }
    public String getCountryName() {
        return countryName;
    }
    public String getCountryLanguage() {
        return countryLanguage;
    }
    public String getCountryCurrency() {
        return countryCurrency;
    }
    public int getCountryPopulation() {
        return countryPopulation;
    }       

}
}

我想这会有所帮助

https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

答案 3 :(得分:0)

此错误消息的原因是以下行:

public void classCountry(...

构造函数必须没有返回值,因此必须删除void。构造函数还必须在大小写方面匹配类定义。因此,将行更改为

public ClassCountry(...

它将起作用。

答案 4 :(得分:0)

类名称为:ClassCountry。 构造函数名称应类似于:public ClassCountry()。 但是,在您的情况下,您认为应该的构造函数实际上并不是构造函数,而是被认为是一种简单的方法。

因此,请更正构造函数名称的拼写,并删除空白部分。

答案 5 :(得分:-1)

您有一个名为ClassCountry的类,因此您的构造函数必须采用

的形式
public ClassCountry(args) {} 

Java是区分大小写的语言,因此classCountry将与构造函数不匹配,并且您的构造函数中还存在一个空位,该空位会创建没有返回值的方法而不是构造函数。