gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:48:59 25 4
gpt4 key购买 nike

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;
}

}

最佳答案

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 .

关于java - 构造函数 ClassCountry(String, String, String, int) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52491799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com