Java 对嵌套类的理解
我正在努力理解以下代码:Java 对嵌套类的理解,java,Java,我正在努力理解以下代码: public class StudentManager { public Student find(String studentID) throws StudentNotFoundException { if (studentID.equals("123456")) { return new Student(); } else { throw new Stud
public class StudentManager {
public Student find(String studentID) throws StudentNotFoundException {
if (studentID.equals("123456")) {
return new Student();
} else {
throw new StudentNotFoundException(
"Could not find student with ID " + studentID);
}
}
}
public Student find(String studentID)抛出StudentNotFoundException
-这是一个嵌套类吗?然后我们使用find(stringstudentid)
a方法?有人能帮我解释一下这个代码吗?为了理解自定义异常处理,我遵循以下步骤
更新:
public class StudentTest {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
try {
Student student = manager.find("0000001");
} catch (StudentNotFoundException ex) {
System.err.print(ex);
}
}
}
它不是嵌套类。它是一个公共实例方法,接受一个
studentID
参数作为String
返回一个Student
,并可能抛出一个StudentNotFoundException
我们可以将其返回类型用作String或Int而不是Student吗?是否要更改返回类型?@AHF在这种情况下,否,因为函数返回类型为Student
的值。但是函数可以返回String
和任何其他类型代码>所以我们在这里做这个函数的对象?还是方法学生的实例?此学生是用户定义的数据类型吗?Student
和StudentManager
似乎都是用户定义的类。在Student=manager.find(“000001”)
,manager
很可能是“StudentManager”类的实例。manager
实例的find(String)
方法调用时使用“000001”
作为参数。查看此find
方法是如何编程的,在这种情况下,它将抛出StudentNotFoundException
。