此时的目标只是找出此代码未编译的原因。下面的类根据某些条件创建一个新的'K12Student'实例,该类是一个超类,它扩展到三个子类,定义了不同类型的学生。每个子类包含唯一的实例变量及其get和set方法。该课程目前如下:
import java.util.*;
//Create New arraylist for client instances
public class StudentInput {
private InputHelper input;
private ArrayList students;
public void run() {
studentInfoEntry();
}
//Assign data to instances of client
public void studentInfoEntry() {
students = new ArrayList();
input = new InputHelper();
String studentIDString = "";
int studentID = 0;
String studentName = "";
String schoolName = "";
String gradeLevelString = "";
int gradeLevel = 0;
String validateAddNewStudent = "";
while (true) {
studentIDString = input.getUserInput("Enter student ID number.");
studentID = Integer.parseInt(studentIDString);
studentName = input.getUserInput("Enter student name.");
schoolName = input.getUserInput("Enter school name.");
gradeLevelString = input.getUserInput("Enter grade level.");
gradeLevel = Integer.parseInt(gradeLevelString);
if (gradeLevel >= 0 && gradeLevel <= 12) {
if (gradeLevel >= 0 && gradeLevel <= 4) {
String readingLevelString = "";
int readingLevel = 0;
String classSection = "";
readingLevelString = input.getUserInput("Enter reading level.");
readingLevel = Integer.parseInt(readingLevelString);
classSection = input.getUserInput("Enter class section.");
/*K12Student*/
PrimaryStudent newStudent = new PrimaryStudent();
newStudent.setStudentID(studentID);
newStudent.setStudentName(studentName);
newStudent.setSchoolName(schoolName);
newStudent.setReadingLevel(readingLevel);
newStudent.setClassSection(classSection);
}
if (gradeLevel >= 5 && gradeLevel <= 8) {
String lunchHourString = "";
int lunchHour = 0;
String homeroomTeacher = "";
lunchHourString = input.getUserInput("Enter lunch hour.");
lunchHour = Integer.parseInt(lunchHourString);
homeroomTeacher = input.getUserInput("Enter homeroom teacher.");
/*K12Student*/
MiddleStudent newStudent = new MiddleStudent();
newStudent.setStudentID(studentID);
newStudent.setStudentName(studentName);
newStudent.setSchoolName(schoolName);
newStudent.setLunchHour(lunchHour);
newStudent.setHomeroomTeacher(homeroomTeacher);
}
if (gradeLevel >= 9 && gradeLevel <= 12) {
String GPAString = "";
int GPA = 0;
String collegeChoice = "";
GPAString = input.getUserInput("Enter reading level.");
GPA = Integer.parseInt(GPAString);
collegeChoice = input.getUserInput("Enter class section.");
K12Student newStudent = new HighStudent();
newStudent.setStudentID(studentID);
newStudent.setStudentName(studentName);
newStudent.setSchoolName(schoolName);
newStudent.setReadingLevel(readingLevel);
newStudent.setClassSection(classSection);
}
students.add(newStudent);
validateAddNewStudent = input.getUserInput("Enter another student? (y/n)");
if (!validateAddNewStudent.equals("y")) {
break;
}
}else{
System.out.println("Grade level must be from 0-12.");
}
}
}
}
此外,每个新中学和小学生的实例化原来都是高中生(K12Student newStudent = new HighStudent
;)的实例,但我有更多的问题用这种格式编译。编译器错误如下:
javac K12StudentTestDrive.java
./StudentInput.java:97: error: cannot find symbol
newStudent.setReadingLevel(readingLevel);
^
symbol: variable readingLevel
location: class StudentInput
./StudentInput.java:98: error: cannot find symbol
newStudent.setClassSection(classSection);
^
symbol: variable classSection
location: class StudentInput
./StudentInput.java:100: error: cannot find symbol
students.add(newStudent);
^
symbol: variable newStudent
location: class StudentInput
3 errors
提前感谢您的任何反馈。
答案 0 :(得分:1)
变量只存在于它们声明的范围内,范围大部分是花括号(还有其他范围规则,但在你的情况下,它是块范围)。您可以在早期if
语句的大括号内定义变量。一旦代码离开if
语句,它们就会超出范围。它们以后不可用。
代码的重要部分是:
if (...) {
int readingLevel = ...;
String classSection = ...;
...
} // <= readingLevel and classSection go out of scope here
if (...) {
K12Student newStudent = ...;
newStudent.setReadingLevel(readingLevel); // <= readingLevel isn't here
newStudent.setClassSection(classSection); // <= classSection isn't here
} // <= newStudent goes out of scope here
students.add(newStudent); // <= newStudent isn't here
请注意,这与抽象类无关。
答案 1 :(得分:0)
变量在Java中具有块级范围。
http://www.java2s.com/Tutorial/Java/0020__Language/VariableScopeinablock.htm
newStudent.setReadingLevel(readingLevel);
无法读取readingLevel
,因为您在一个块中声明了它,然后退出该块,该变量使变量readingLevel
超出范围。基本上它会在那时停止存在。如果你想让变量更长寿,你需要将声明移到它当前所在的块之外。