KLF. 2023-04-21 21:16 采纳率: 28.6%
浏览 25
已结题

Java报错Error occurred during initialization of boot layer怎么办

运行报错,这为啥呀,弄好几个小时了/(ㄒoㄒ)/~~
代码:

package test0420;

abstract class Person {
    protected String name;
    protected String sex;
    protected String birth;
    public Person() {}
    public Person(String name,String sex,String birth) {
        this.name=name;
        this.sex=sex;
        this.birth=birth;
        }
    public String toString() {
        return "姓名:"+name+",性别:"+sex+",出生日期:"+birth;
    }
    }
class Teacher extends Person{
    private String school;
    private String kind;
    public Teacher(String name,String sex,String birth,String school,String kind) {
        super(name,sex,birth);
        this.school=school;
        this.kind=kind;
    }
    public String toString() {
        return super.toString()+",任课学校:"+school+",类别:"+kind;
    }
}
abstract class Student extends Person{
    protected String school;
    protected int sno;
    protected String grade;
    public Student () {}
    public Student (String name,String sex,String birth,String school,int sno,String grade) {
        super(name,sex,birth);
        this.school=school;
        this.sno=sno;
        this.grade=grade;
    }
    public String toString() {
        return super.toString()+",学校"+school+",学号:"+sno+",年级:"+grade;
    }
}
class College extends Student{
    private String major;
    public College() {}
    public College(String name,String sex,String birth,String school,int sno,String grade,String major) {
        super(name,sex,birth,school,sno,grade);
        this.major=major;
    }
    public String toString() {
        return super.toString()+",专业:"+major;
    }
}
class Middle extends Student{
    public Middle() {}
    public Middle(String name,String sex,String birth,String school,int sno,String grade) {
        super(name,sex,birth,school,sno,grade);
    }
    public String toString() {
    return super.toString();
}
class Primary extends Student{
    public Primary() {}
    public Primary(String name,String sex,String birth,String school,int sno,String grade) {
        super(name,sex,birth,school,sno,grade);
        this.school=school;
        
    }
    public String toString() {
    return super.toString();
    }
}
public class Test{
    public void main(String[] args) {
    Teacher t[]=new Teacher[2];
    t[0]= new Teacher("王老师", "女", "1991年5月3日", "南昌交通学院", "大学");
    t[2]= new Teacher("李老师", "男", "1981年8月5日", "实验小学", "小学");
    System.out.println(t.toString());
    College c[]=new College[3];
    c[0]= new College("张三", "男","2003年7月9日", "南昌大学",001, "大二", "计算机");
    c[1]= new College("李四", "男","2001年6月12日", "南昌交通学院",045, "大四", "软件工程");
    c[2]= new College("黄玲", "女","2003年12月4日", "厦门大学",076, "大一", "英语");
    System.out.println(c.toString());
    Middle m[]=new Middle[3];
    m[0]= new Middle("李华", "女","2004年11月8日", "永新中学",024, "高三");
    m[1]= new Middle("尹龙", "男","2003年6月15日", "永新二中",024, "高二");
    m[2]= new Middle("陈萧", "男","2003年3月6日", "禾川中学",024, "高一");
    System.out.println(m.toString());
    Primary p[]=new Primary[3];
    p[0]= new Primary("黄栗","女","2012年6月30日","城厢小学",034,"三年级");
    p[1]= new Primary("陈泽","男","2010年5月18日","城南小学",024,"一年级");
    System.out.println(p.toString());
    }
}

报错:

img

  • 写回答

1条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-04-21 21:22
    关注
    • Test 类 和 Primary 要独立出来,跟其他类平级,你都写到 Middle 类里了
    • 另外Test类中 的 main 方法 没有用 static 修饰,它是无法运行的
    • Teather数组 ,访问 t[2] 肯定会数组越界的
    • 直接打印 数组,不能调用 数组元素的toString方法,你要换成 Arrays.toString
    • 帮你修改如下:
    
    package test0420;
    import java.util.Arrays;
    
    /**
     * @author huazie
     * @version 2.0.0
     * @since 2.0.0
     */
    
    abstract class Person {
        protected String name;
        protected String sex;
        protected String birth;
    
        public Person() {
        }
    
        public Person(String name, String sex, String birth) {
            this.name = name;
            this.sex = sex;
            this.birth = birth;
        }
    
        public String toString() {
            return "姓名:" + name + ",性别:" + sex + ",出生日期:" + birth;
        }
    }
    
    class Teacher extends Person {
        private String school;
        private String kind;
    
        public Teacher(String name, String sex, String birth, String school, String kind) {
            super(name, sex, birth);
            this.school = school;
            this.kind = kind;
        }
    
        public String toString() {
            return super.toString() + ",任课学校:" + school + ",类别:" + kind;
        }
    }
    
    abstract class Student extends Person {
        protected String school;
        protected int sno;
        protected String grade;
    
        public Student() {
        }
    
        public Student(String name, String sex, String birth, String school, int sno, String grade) {
            super(name, sex, birth);
            this.school = school;
            this.sno = sno;
            this.grade = grade;
        }
    
        public String toString() {
            return super.toString() + ",学校" + school + ",学号:" + sno + ",年级:" + grade;
        }
    }
    
    class College extends Student {
        private String major;
    
        public College() {
        }
    
        public College(String name, String sex, String birth, String school, int sno, String grade, String major) {
            super(name, sex, birth, school, sno, grade);
            this.major = major;
        }
    
        public String toString() {
            return super.toString() + ",专业:" + major;
        }
    }
    
    class Middle extends Student {
        public Middle() {
        }
    
        public Middle(String name, String sex, String birth, String school, int sno, String grade) {
            super(name, sex, birth, school, sno, grade);
        }
    
        public String toString() {
            return super.toString();
        }
    }
    
    class Primary extends Student {
        public Primary() {
        }
    
        public Primary(String name, String sex, String birth, String school, int sno, String grade) {
            super(name, sex, birth, school, sno, grade);
            this.school = school;
    
        }
    
        public String toString() {
            return super.toString();
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Teacher t[] = new Teacher[2];
            t[0] = new Teacher("王老师", "女", "1991年5月3日", "南昌交通学院", "大学");
            t[1] = new Teacher("李老师", "男", "1981年8月5日", "实验小学", "小学");
            System.out.println(Arrays.toString(t));
            College c[] = new College[3];
            c[0] = new College("张三", "男", "2003年7月9日", "南昌大学", 001, "大二", "计算机");
            c[1] = new College("李四", "男", "2001年6月12日", "南昌交通学院", 045, "大四", "软件工程");
            c[2] = new College("黄玲", "女", "2003年12月4日", "厦门大学", 076, "大一", "英语");
            System.out.println(Arrays.toString(c));
            Middle m[] = new Middle[3];
            m[0] = new Middle("李华", "女", "2004年11月8日", "永新中学", 024, "高三");
            m[1] = new Middle("尹龙", "男", "2003年6月15日", "永新二中", 024, "高二");
            m[2] = new Middle("陈萧", "男", "2003年3月6日", "禾川中学", 024, "高一");
            System.out.println(Arrays.toString(m));
            Primary p[] = new Primary[3];
            p[0] = new Primary("黄栗", "女", "2012年6月30日", "城厢小学", 034, "三年级");
            p[1] = new Primary("陈泽", "男", "2010年5月18日", "城南小学", 024, "一年级");
            System.out.println(Arrays.toString(p));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上