找不到符号,符号:方法

时间:2019-01-05 02:45:23

标签: java methods symbols

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

错误: 找不到标志     test.say();

符号:方法say()

class Untitled {
  public void main(String[] args) {
    Student test = new Student();
    test.say();
  }
  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);
  }
}

谢谢

1 个答案:

答案 0 :(得分:1)

方法say()实际上是在您的Untitled类上定义的,而不是在您的Student类上定义的,因此是编译器警告。

您可能想将main()方法更改为以下内容:

public void main(String[] args) {
  Untitled test = new Untitled();
  test.say();
}

或者,您可以这样将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()以打印当前值,而不要创建新的Student。像这样:

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