java - 无效的方法声明;需要返回类型 [英] java - invalid method declaration; return type required
问题描述
在Java OOP项目中,我的构造函数出现了三个错误:
On a Java OOP project I got three errors on my constructor:
。\ Voter.java:14:错误:无效方法声明;返回类型
必需
.\Voter.java:14: error: invalid method declaration; return type required
。\ Candandates.java:7:错误:方法声明无效;返回类型
必需
.\Candidates.java:7: error: invalid method declaration; return type required
。\ Candandates.java:14:错误:方法声明无效;返回类型
必需
.\Candidates.java:14: error: invalid method declaration; return type required
构造函数代码:
public class Voter{
private String name;
private int votNum;
private int precint;
public Voter(String name, int votNum, int precint)
{
this.name = name;
this.votNum = votNum;
this.precint = precint;
}
public setDetails(String name, int votNum, int precint)
{
this.name = name;
this.votNum = votNum;
this.precint = precint;
}...}
public class Candidates
{
public String candName;
private int position;
private int totalVotes;
public Candidate (String candName, int position, int totalVotes)
{
this.candName = candName;
this.position = position;
this.totalVotes = totalVotes;
}
public setDetails (String candName, int position, int totalVotes)
{
this.candName = candName;
this.position = position;
this.totalVotes = totalVotes;
}...}
我声明我的构造函数是这样的:
i declared my constructors like this:
public class MainClass{
public static void main(String[] args){
System.out.println("Previous voter's info: ");
Voter vot1 = new Voter("voter name", 131, 1);
System.out.println("The Candidates: ");
Candidates cand1 = new Candidates("candidate name", 1, 93);
}
}
我错过了什么?
推荐答案
在您的方法 setDetails
中,您没有为返回类型指定任何内容,如果不是返回任何内容然后指定 void
In your method setDetails
you haven't specified anything for the return type, if it is not returning anything then specify void
对于选民
class
public void setDetails(String name, int votNum, int precint)
候选人
class
public void setDetails (String candName, int position, int totalVotes)
另一件事,(感谢 Frank Pavageau )您的班级名称是候选人
你已经用 Candidate
定义了构造函数而没有 s
,这就是为什么它是被认为是一种常规方法,因此应具有返回类型。您将构造函数重命名为 Candidates
,或将您的类重命名为 Candidate
哪个更好。
One other thing, (Thanks to Frank Pavageau) your class name is Candidates
and you have defined the constructor with Candidate
without s
, that is why it is being considered as a normal method, and thus should have a return type. You your rename your constructor as Candidates
, or rename your class as Candidate
which is better.
这篇关于java - 无效的方法声明;需要返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!