我在学校包中有两个班级
班级学校
package school;
public class people
{
String Name = null;
public String getName()
{
return "Super Class Name : " + Name;
}
public void setName(String name)
{
Name = name;
}
}
班级学生
package school;
public class students extends people
{
public static void main(String[] args)
{
people objpeople1 = new people();
people objpeople2 = new students();
objpeople1.setName("David");
objpeople2.setName("Davis");
System.out.println(objpeople1.getName());
System.out.println(objpeople2.getName());
}
@Override
public String getName()
{
return "Child Class Name is: "+ getName();
}
}
第一个 getName 方法工作正常。当我尝试使用第二个方法时,它会生成异常。
objpeople2.getName() 正在生成 java.lang.StackOverflowError 异常