Java 在尝试进行构造函数链接时,我不断得到“错误:找不到符号”
这是我的代码中包含构造函数的部分。如您所见,其目的是让构造函数接受较少的参数并调用最具体的构造函数。我想我是在第一个构造函数中创建我的值的。为什么它找不到我的符号 这是我的代码:Java 在尝试进行构造函数链接时,我不断得到“错误:找不到符号”,java,multiple-constructors,Java,Multiple Constructors,这是我的代码中包含构造函数的部分。如您所见,其目的是让构造函数接受较少的参数并调用最具体的构造函数。我想我是在第一个构造函数中创建我的值的。为什么它找不到我的符号 这是我的代码: public class Frog{ // instance variables private String name; private int age; private double tongueSpeed; private boolean isFrogLet; private String s
public class Frog{
// instance variables
private String name;
private int age;
private double tongueSpeed;
private boolean isFrogLet;
private String species;
**// Third constructor**
public Frog( String Name){
this(Name, Age, TongueSpeed, IsFrogLet, Species);
}
**//second constructor**
public Frog(String Name, int ageInYears, double TongueSpeed){
this(Name, Age, TongueSpeed, IsFrogLet, Species);
name= Name;
age = ageInYears;
tongueSpeed= TongueSpeed;
}
**// most specific constructor**
public Frog( String Name, int age, double TongueSpeed, boolean IsFrogLet, String Species){
name = Name;
this.age = Age;
tongueSpeed = TongueSpeed;
isFrogLet= IsFrogLet;
species = Species;
}
public void grow(int months){
age = age + months;
while ( age < 12){
tongueSpeed++;
}
if (age>5 & age>30){
double highRes= age-30;
tongueSpeed= tongueSpeed-highRes;
}
if (age>1 & age <7){
isFrogLet = true;
}
}
}
我认为您对构造函数重载的概念理解错误。**//第三个构造函数**只接受名称,但您仍在尝试将其他内容传递给它。如果东西不存在,你就不能传递。因此,传递来自参数的内容,并将其他内容设置为null,如下所示:
这同样适用于其他构造函数。查看具体构造函数的参数是什么,并将其他内容设置为null。我认为您对构造函数重载的概念理解错误。**//第三个构造函数**只接受名称,但您仍在尝试将其他内容传递给它。如果东西不存在,你就不能传递。因此,传递来自参数的内容,并将其他内容设置为null,如下所示:
这同样适用于其他构造函数。查看具体构造函数的参数是什么,并将其他内容设置为null。错误是不言自明的。在将变量传递给最特定的构造函数之前,不能在第二个和第三个构造函数中创建具有特定名称的变量。 您应该将在构造函数中接收的确切变量传递给基础构造函数,或者将一些默认值传递给最特定的构造函数,而不是这些变量或符号。 因此,您应该通过以下方式调用第二个和第三个构造函数:
// Third constructor
public Frog(String Name) {
this(Name, 0, 0.0, false, "");
}
//second constructor
public Frog(String Name, int ageInYears, double TongueSpeed) {
this(Name, ageInYears, TongueSpeed, false, "");
}
希望这有帮助。错误是不言自明的。在将变量传递给最特定的构造函数之前,不能在第二个和第三个构造函数中创建具有特定名称的变量。 您应该将在构造函数中接收的确切变量传递给基础构造函数,或者将一些默认值传递给最特定的构造函数,而不是这些变量或符号。 因此,您应该通过以下方式调用第二个和第三个构造函数:
// Third constructor
public Frog(String Name) {
this(Name, 0, 0.0, false, "");
}
//second constructor
public Frog(String Name, int ageInYears, double TongueSpeed) {
this(Name, ageInYears, TongueSpeed, false, "");
}
希望这有帮助
错误原因
您使用的是不存在的变量,因为您的实例级别在camelCase中没有标题Case变量,除此之外请查找重构代码并尝试使用camelCase作为变量
public class Frog {
private String name;
private int age;
private double tongueSpeed;
private boolean isFrogLet;
private String species;
public Frog(String name, int age, double tongueSpeed, boolean isFrogLet, String species) {
this.name = name;
this.age = age;
this.tongueSpeed = tongueSpeed;
this.isFrogLet = isFrogLet;
this.species = species;
}
public Frog(String name) {
this.name = name;
}
public Frog(String name, int ageInYears, double TongueSpeed) {
this.name = name;
this.age = ageInYears;
this.tongueSpeed = TongueSpeed;
}
public void grow(int months) {
age = age + months;
while (age < 12) {
tongueSpeed++;
}
if (age > 5 & age > 30) {
double highRes = age - 30;
tongueSpeed = tongueSpeed - highRes;
}
if (age > 1 & age < 7) {
isFrogLet = true;
}
}
}
错误原因
您使用的是不存在的变量,因为您的实例级别在camelCase中没有标题Case变量,除此之外请查找重构代码并尝试使用camelCase作为变量
public class Frog {
private String name;
private int age;
private double tongueSpeed;
private boolean isFrogLet;
private String species;
public Frog(String name, int age, double tongueSpeed, boolean isFrogLet, String species) {
this.name = name;
this.age = age;
this.tongueSpeed = tongueSpeed;
this.isFrogLet = isFrogLet;
this.species = species;
}
public Frog(String name) {
this.name = name;
}
public Frog(String name, int ageInYears, double TongueSpeed) {
this.name = name;
this.age = ageInYears;
this.tongueSpeed = TongueSpeed;
}
public void grow(int months) {
age = age + months;
while (age < 12) {
tongueSpeed++;
}
if (age > 5 & age > 30) {
double highRes = age - 30;
tongueSpeed = tongueSpeed - highRes;
}
if (age > 1 & age < 7) {
isFrogLet = true;
}
}
}
对于基本数据类型int、boolean和double,如何传递null?您是正确的。应该花更多的时间研究代码。编辑了我的答案。我很高兴我提供了帮助:对于int、boolean和double等基本数据类型,如何传递null?你是对的。应该花更多的时间研究代码。编辑了我的答案。我很高兴我提供了帮助:您应该遵循Java命名约定:变量名称和方法名称应该用camelCase编写。您应该遵循Java命名约定:变量名称和方法名称应该用camelCase编写。