简体   繁体   English

无效的方法声明:构造函数中的“需要返回类型”

[英]Invalid method declaration: “return type required” in constructor

My problem is here between public and StudentInfo : 我的问题在publicStudentInfo之间:

public class Student_Database{
  public StudentInfo(int IDnum,String Name,String Year) {
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
  }
}

It looks like you're trying to create a constructor, but the compiler doesn't see this as one. 看起来您正在尝试创建一个构造函数,但编译器并不认为这是一个构造函数。 I'm guessing but likely your constructor name isn't exactly matching your class name. 我正在猜测,但可能您的构造函数名称与您的类名称不完全匹配。 If so, fix this. 如果是这样,请解决此问题。 Make them match -- the class name must exactly match that of the constructor, including spelling and capitalization. 使它们匹配-类名称必须与构造函数的名称完全匹配,包括拼写和大小写。

As an aside, you'll want to learn and adhere to Java naming conventions, including giving all variables and parameters names that start with lower-case letters. 顺便说一句,您将要学习并遵守Java命名约定,包括为所有变量和参数提供以小写字母开头的名称。 So your field names should be idNum, name, and year. 因此,您的字段名称应为idNum,名称和年份。


Edit 编辑
You state that the class declaration is: public class Student_Database{} . 您声明类声明为: public class Student_Database{} If so, then the constructor should look like so: 如果是这样,则构造函数应如下所示:

public Student_Database(int IDnum,String Name,String Year)
{
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
}

Although better would be I think to keep it as StudentInfo, and have it look like: 虽然更好,我认为将其保留为StudentInfo,并使其看起来像:

public class StudentInfo {
  private int idNumber;
  private String name;
  private String year;

  public StudentInfo(int idNumber, String name, String year) {
     this.idNumber = idNumber;
     this.name = name;
     this.year = year;
  }

  // getters, setters, toString, equals override, hashCode override

}

I guess you may want to create a method to set StudentInfo 我想您可能想创建一种设置StudentInfo的方法

public void setStudentInfo(int IDnum,String Name,String Year)
{
    this.IDnum = IDnum;
    this.Name = Name;
    this.Year = Year;       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM