publicclassa{publicstaticvoidmain(String[]j){Studentstudent=newStudent();student.setId(100);student.setName("是的");Student.setAge(20);System.out.println("id="+student.id...
public class a{
public static void main(String [] j ){
Student student = new Student ();
student.setId(100);
student.setName("是的");
Student.setAge(20);
System.out.println("id=" + student.id);
System.out.println("name=" + student.name);
System.out.println("age=" + student.age);
}
}
class Student{
int id;
String name;
int age;
public int getId(){
return id;
}
public void setId(int studentId){
id = studentId;
}
public String getName(){
return name;
}
public void setName(String studentName){
name = studentName;
}
public int getAge(){
return age;
}
public void setAge(int studentAge){
if (studentAge > 0){
age=studentAge;
}
}
}
和书上写的一样,为什么在命令提示符里提示:
G:\>javac a.java
a.java:8: 无法从静态上下文中引用非静态 方法 setAge(int)
Student.setAge(20);
^
1 错误
G:\>
刚接触JAVA 请高手指点一下, 谢谢了。
展开