首页 > 解决方案 > 错误:学生班中的构造函数学生不能应用于给定类型;超级(姓名,注册);

问题描述

我一开始就宣布了超级权利。但它仍然显示它不是的错误。父类中的构造函数也与“super()”中的构造函数具有相同的参数。仍然说参数不匹配

 import java.io.*;
    import java.util.Scanner;
    
     class student
    {
      public String name;
      public int enroll;
    
      public void student(String name, int enroll)
      {
        this.name=name;
        this.enroll=enroll;
      }
    }
    class Science extends student
    {
      int phy;
      int chem;
      int maths;
      public void Science(String name, int enroll, int phy, int chem, int maths)
      {
        super(name,enroll);
        this.phy=phy;
        this.chem=chem;
        this.maths=maths;
      }
    }
    class Arts extends student
    {
      int eng;
      int hist;
      int eco;
      public void Arts(String name, int enroll, int eng, int hist, int eco)
      {
        super(name,enroll);
        this.eng=eng;
        this.hist=hist;
        this.eco=eco;
      }
      
    }
    public class asd
    {
      //enter code here
      public static void main(String[] args)  throws IOException
      {
        //enter code here
       } 
      
    }

标签: javascriptjavainheritanceconstructor

解决方案


正如评论中所指出的,您需要void构造函数中删除,因为它们没有任何明确的返回类型。您可以检查此答案以找出构造函数不返回值的原因

因此,您当前的构造函数如下所示:

public void student(String name, int enroll)
{
    this.name=name;
    this.enroll=enroll;
}

应该看起来像这样,类名也应该按照标准 java 约定以大写字母开头:

class Student {

  public Student(String name, int enroll)
  {
    this.name=name;
    this.enroll=enroll;
  }

推荐阅读