如何清除错误并使方法正常工作?
错误: 找不到标志 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);
}
}
谢谢
答案 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);
}
}