鱼C论坛

 找回密码
 立即注册
查看: 4349|回复: 3

继承时报错了

[复制链接]
发表于 2020-12-16 12:03:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

Student是基类,Menu类要继承Student

这个是Student.java文件的代码
  1. package essential;
  2. public class Student {
  3.        
  4.         public String studentNumber;
  5.         public String name;
  6.         public String major;
  7.        
  8.         public int math;
  9.         public int computer;
  10.         public int english;
  11.         public int sum;
  12.        
  13.         public Student (String _studentNumber, String _name, String _major, int _math, int _computer, int _english){
  14.                 studentNumber = _studentNumber;
  15.                 name = _name;
  16.                 major = _major;
  17.                
  18.                 math = _math;
  19.                 computer = _computer;
  20.                 english = _english;
  21.         }
  22. }
复制代码


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

  4.         public static void main(String[] args) {
  5.                 Scanner in = new Scanner(System.in);
  6.                 Student []stu = new Student[5];
  7.                
  8.                 stu[0] = new Student("Jack","软工","20183101",80,90,85);
  9.                 stu[1] = new Student("Rose","大数据","20183102",99,93,90);
  10.                 stu[2] = new Student("John","网安全","20183103",87,70,74);
  11.                 stu[3] = new Student("Andi","网工程","20183104",67,66,68);
  12.                 stu[4] = new Student("Mike","物联网","20183105",56,90,55);
  13.                
  14.         }
  15. }
复制代码

这个该怎么解决呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-16 12:10:13 | 显示全部楼层
如果加上构造函数,改为这样,就找不到Student了
  1. package essential;
  2. import java.util.Scanner;
  3. public class Menu extends Student {

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

  8.         public static void main(String[] args) {
  9.                 Scanner in = new Scanner(System.in);
  10.                 Student []stu = new Student[5];
  11.                
  12.                 stu[0] = new Student("Jack","软工","20183101",80,90,85);
  13.                 stu[1] = new Student("Rose","大数据","20183102",99,93,90);
  14.                 stu[2] = new Student("John","网安全","20183103",87,70,74);
  15.                 stu[3] = new Student("Andi","网工程","20183104",67,66,68);
  16.                 stu[4] = new Student("Mike","物联网","20183105",56,90,55);
  17.                
  18.         }
  19. }
复制代码

报错为public class Menu extends Student {
                          ^
  符号: 类 Student
Menu.java:12: 错误: 找不到符号
                Student []stu = new Student[5];
                ^
  符号:   类 Student
  位置: 类 Menu
Menu.java:12: 错误: 找不到符号
                Student []stu = new Student[5];
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 14:50:47 From FishC Mobile | 显示全部楼层
Menu类写个构造函数,用super(Student构造函数需要的参数)调用student类的构造函数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-22 18:50:14 | 显示全部楼层
baige 发表于 2020-12-22 14:50
Menu类写个构造函数,用super(Student构造函数需要的参数)调用student类的构造函数

不好意思,之前忘记修改帖子了,是环境变量的问题,已经解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 17:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表