java - 无效的方法声明;需要返回类型

标签 java oop

<分区>

在 Java OOP 项目中,我的构造函数出现了三个错误:

.\Voter.java:14: error: invalid method declaration; return type required

.\Candidates.java:7: error: invalid method declaration; return type required

.\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;
    }...}

我这样声明我的构造函数:

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

对于 Voter

public void setDetails(String name, int votNum, int precint)

对于候选人

public void setDetails (String candName, int position, int totalVotes)

另一件事,(感谢 Frank Pavageau) 你的类名是 Candidates 并且你定义了带有 Candidate 的构造函数而不是 s,这就是为什么它被认为是一个普通方法,因此应该有一个返回类型。您将构造函数重命名为 Candidates,或者将您的类重命名为 Candidate,哪个更好。

关于java - 无效的方法声明;需要返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15698239/

相关文章:

java - 通过父类(super class)返回与类名相同的数据类型?

java - 使用 Reasoner 对象提取披萨本体中的直接父类(super class)

php - 接口(interface)或抽象类 : which one to use?

java - 当文件中存在重复的键值对时,如何读取属性文件?

java - java中设备的自动网络发现

java - 如何添加类的匿名实例作为 ActionListener

c++ - 无法转换某些类,但其他类可以。 C++

java - 如何比较对象数组的值?

java - Apache FTPClient for Java 显示运行的 FTP 命令

java - Spring LibGDX And​​roid 应用程序中缺少 java.beans.Introspector