Java 找不到符号,符号:方法

Java 找不到符号,符号:方法,java,methods,symbols,Java,Methods,Symbols,如何消除错误并使方法正常工作 错误: 找不到符号 测试。说() 符号:方法say() 谢谢方法say()实际上是在无标题的类上定义的,而不是在学生的类上定义的,因此编译器发出警告 您可能希望将main()方法更改为类似以下内容: public void main(String[] args) { Untitled test = new Untitled(); test.say(); } public class Student { public String name = "Joh

如何消除错误并使方法正常工作

错误: 找不到符号 测试。说()

符号:方法say()

谢谢方法
say()
实际上是在
无标题的
类上定义的,而不是在
学生的
类上定义的,因此编译器发出警告

您可能希望将
main()
方法更改为类似以下内容:

public void main(String[] args) {
  Untitled test = new Untitled();
  test.say();
}
public class Student {
  public String name = "John";
  public String studentindex = "23";
  public String group = "11c";    
  public void say () {
    System.out.println(this.name);
    System.out.println(this.studentindex);
    System.out.println(this.group);
  }
}
或者,您可以移动
say()
“内部”
Student
,这样:

public class Student {
  public String name = "John";
  public String studentindex = "23";
  public String group = "11c";    
  public void say () {
    Student stud = new Student();
    System.out.println(stud.name);
    System.out.println(stud.studentindex);
    System.out.println(stud.group);
  }
}
如果您采用这种方法,那么我建议更新
say()
以打印当前值,而不是创建新的
学生
。比如:

public void main(String[] args) {
  Untitled test = new Untitled();
  test.say();
}
public class Student {
  public String name = "John";
  public String studentindex = "23";
  public String group = "11c";    
  public void say () {
    System.out.println(this.name);
    System.out.println(this.studentindex);
    System.out.println(this.group);
  }
}

say()不在Student类的作用域中。它只是缺少一个
}
新的无标题()say()
您可以使
say
也使用
this
而不是在实例方法中创建另一个实例。这很奇怪,我同意。我在想“一步一个脚印”。不过我会更新我的答案。