小甲鱼的铁粉 发表于 2020-12-16 12:03:11

继承时报错了

本帖最后由 小甲鱼的铁粉 于 2020-12-16 12:04 编辑

Student是基类,Menu类要继承Student
这个是Student.java文件的代码
package essential;
public class Student {
       
        public String studentNumber;
        public String name;
        public String major;
       
        public int math;
        public int computer;
        public int english;
        public int sum;
       
        public Student (String _studentNumber, String _name, String _major, int _math, int _computer, int _english){
                studentNumber = _studentNumber;
                name = _name;
                major = _major;
               
                math = _math;
                computer = _computer;
                english = _english;
        }
}


这个是Menu.java文件的代码,红色的是报错的部分,public class Menu extends Student报错为:没有为缺省构造函数定义隐式超构造函数 Student()。必须定义显式构造函数
package essential;
import java.util.Scanner;
public class Menu extends Student{

        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                Student []stu = new Student;
               
                stu = new Student("Jack","软工","20183101",80,90,85);
                stu = new Student("Rose","大数据","20183102",99,93,90);
                stu = new Student("John","网安全","20183103",87,70,74);
                stu = new Student("Andi","网工程","20183104",67,66,68);
                stu = new Student("Mike","物联网","20183105",56,90,55);
               
        }
}

这个该怎么解决呢{:10_266:}

小甲鱼的铁粉 发表于 2020-12-16 12:10:13

如果加上构造函数,改为这样,就找不到Student了
package essential;
import java.util.Scanner;
public class Menu extends Student {

        public Menu(String _studentNumber, String _name, String _major, int _math, int _computer, int _english) {
                super(_studentNumber, _name, _major, _math, _computer, _english);
                // TODO 自动生成的构造函数存根
        }

        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                Student []stu = new Student;
               
                stu = new Student("Jack","软工","20183101",80,90,85);
                stu = new Student("Rose","大数据","20183102",99,93,90);
                stu = new Student("John","网安全","20183103",87,70,74);
                stu = new Student("Andi","网工程","20183104",67,66,68);
                stu = new Student("Mike","物联网","20183105",56,90,55);
               
        }
}

报错为public class Menu extends Student {
                        ^
符号: 类 Student
Menu.java:12: 错误: 找不到符号
                Student []stu = new Student;
                ^
符号:   类 Student
位置: 类 Menu
Menu.java:12: 错误: 找不到符号
                Student []stu = new Student;

baige 发表于 2020-12-22 14:50:47

Menu类写个构造函数,用super(Student构造函数需要的参数)调用student类的构造函数

小甲鱼的铁粉 发表于 2020-12-22 18:50:14

baige 发表于 2020-12-22 14:50
Menu类写个构造函数,用super(Student构造函数需要的参数)调用student类的构造函数

不好意思,之前忘记修改帖子了,是环境变量的问题,已经解决了
页: [1]
查看完整版本: 继承时报错了