这是我试过的一个Java程序。但是我找不到我把代码弄错的地方
在main函数中执行此操作这是我试过的一个Java程序。但是我找不到我把代码弄错的地方,java,Java,在main函数中执行此操作 StudentResult是ExamDetails类的内部类,而不是静态类 因此,要在静态主函数中使用非静态成员,需要引用包含该成员的类。进行以下更改: StudentResults aStudent = new ExamDetails().new StudentResults(); 或者第二个选项是在单独的文件中声明StudentResults另一个选项: public class ExamDetails { static StudentResult
StudentResult
是ExamDetails
类的内部类,而不是静态类
因此,要在静态主函数中使用非静态成员,需要引用包含该成员的类。进行以下更改:
StudentResults aStudent = new ExamDetails().new StudentResults();
或者第二个选项是在单独的文件中声明StudentResults
另一个选项:
public class ExamDetails {
static StudentResults aStudent = new StudentResults();
public static void main (String[] args){
String sName = aStudent.fullName("Bill Gates");
System.out.println(sName);
}
static class StudentResults{
private String Full_Name;
private String Exam_Name;
private String Exam_Score;
private String Exam_Grade;
StudentResults(){
Full_Name = "No Name Given";
Exam_Name = "Unknown";
Exam_Score = "No Score";
Exam_Grade = "Unknown";
}
String fullName(String aName){
Full_Name = aName;
return Full_Name;
}
}
}
那么问题出在哪里呢?尝试向StudentResults类添加static关键字。类似这样的内容:静态类StudentResults{…}
public class ExamDetails {
static StudentResults aStudent = new StudentResults();
public static void main (String[] args){
String sName = aStudent.fullName("Bill Gates");
System.out.println(sName);
}
static class StudentResults{
private String Full_Name;
private String Exam_Name;
private String Exam_Score;
private String Exam_Grade;
StudentResults(){
Full_Name = "No Name Given";
Exam_Name = "Unknown";
Exam_Score = "No Score";
Exam_Grade = "Unknown";
}
String fullName(String aName){
Full_Name = aName;
return Full_Name;
}
}
}
package various;
public class StudentResults{
public static void main (String[] args){
StudentResults aStudent = new StudentResults();
String sName = aStudent.fullName("Bill Gates");
System.out.println(sName);
}
private String Full_Name;
private String Exam_Name;
private String Exam_Score;
private String Exam_Grade;
StudentResults(){
Full_Name = "No Name Given";
Exam_Name = "Unknown";
Exam_Score = "No Score";
Exam_Grade = "Unknown";
}
String fullName(String aName){
Full_Name = aName;
return Full_Name;
}
}