【问题标题】:I keep getting "error: cannot find symbol" when trying to do constructor chaining尝试进行构造函数链接时,我不断收到“错误:找不到符号”
【发布时间】:2020-06-20 20:47:33
【问题描述】:

所以这是我的代码中包含构造函数的部分。如您所见,目的是让构造函数接受更少的参数并调用最具体的构造函数。我以为我在第一个构造函数中启动了我的值。为什么找不到我的符号?

这是我的代码:

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

}

-这是我的错误:

Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
            ^
  symbol:   variable Age
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                 ^
  symbol:   variable TongueSpeed
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                              ^
  symbol:   variable IsFrogLet
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                         ^
  symbol:   variable Species
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
              ^
  symbol:   variable Age
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                ^
  symbol:   variable IsFrogLet
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                           ^
  symbol:   variable Species
  location: class Frog
Frog.java:28: error: cannot find symbol
   this.age = Age;
              ^
  symbol:   variable Age
  location: class Frog

【问题讨论】:

  • 你应该遵循 Java 命名约定:变量名和方法名应该用驼峰命名。

标签: java multiple-constructors


【解决方案1】:

我认为您对构造函数重载的概念有误。 **// Third constructor** 只接收名称,但您仍在尝试将其他内容传递给 this。如果东西不存在,你就不能传递它。因此,传递来自参数的内容并将其他内容设置为null,如下所示:

public Frog( String Name){
   this(Name, 0, 0.0, false, null));
}

同样适用于其他构造函数。看看具体构造函数的参数是什么,其他的设置成null

【讨论】:

  • 如何将null 传递给原始数据类型(intbooleandouble)?
  • 你是对的。应该花更多时间研究代码。编辑了我的答案。
  • 很高兴我能帮上忙 :)
【解决方案2】:

错误是不言自明的。在将它们传递给最具体的构造函数之前,您不会在第二个和第三个构造函数中创建具有特定名称的变量。
您应该将在构造函数中收到的确切变量传递给底层构造函数,或者您应该将一些默认值传递给最具体的构造函数,而不是这些变量(又名符号)。
所以你应该通过以下方式调用你的第二个和第三个构造函数:

// 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, "");
}  

希望对你有帮助

【讨论】:

    【解决方案3】:

    错误原因

    您正在使用不存在的变量,因为您的实例级别在 camelCase 中没有在标题中除此之外的案例变量请查找重构代码并尝试使用 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;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多