The constructor never completes, therefore the assignment never occurs. It's not that null is returned from the constructor (or that there's a "null object" - there's no such concept). It's just that you never assign a new value to teacher
, so it retains its previous value.
For example, if you use:
Teacher teacher = new Teacher("This is valid", new Student());
Student st = new Student();
try
{
teacher = new Teacher("", st);
}
catch (... etc ...)
... then you'll still have the "This is valid" teacher. The name
variable still won't be assigned a value in that Teacher
object though, as your Teacher
constructor is missing a line such as:
this.name = name;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…