【问题标题】:java - invalid method declaration; return type required [duplicate]java - 无效的方法声明;需要返回类型[重复]
【发布时间】:2013-03-29 06:05:39
【问题描述】:

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

.\Voter.java:14:错误:方法声明无效;返回类型 必填

.\Candidates.java:7:错误:方法声明无效;返回类型 必填

.\Candidates.java:14:错误:方法声明无效;返回类型 必填

构造函数代码:

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);
    }
}

我错过了什么?

【问题讨论】:

    标签: java oop


    【解决方案1】:

    在您的方法setDetails 中,您没有为返回类型指定任何内容,如果它没有返回任何内容,则指定void

    对于Voter

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

    对于Candidates

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

    另外一件事,(感谢Frank Pavageau你的类名是Candidates,并且你用Candidate定义了没有s的构造函数,这就是考虑它的原因作为普通方法,因此应该有一个返回类型。您将构造函数重命名为Candidates,或者将您的类重命名为Candidate,这样会更好。

    【讨论】:

    • 这样做了,但我得到另一个错误,voteDemo.java:8: error: constructor Candidates in class Candidates cannot be ap plied to given types; Candidates cand1 = new Candidates("candidate name", 1, 93);
    • @Ella 你的构造函数叫做 Candidate,它缺少 s 来匹配类名。
    • @Ella 顺便说一下,这个类可能应该被称为候选人(单数)。
    • @Ella,你的类名是Candidates,你的构造函数是Candidate
    • 现在可以使用了,谢谢! C:
    【解决方案2】:

    您的Voter.setDetails 函数没有返回类型。如果您不希望它返回,请将返回类型指定为void

    public void setDetails(String name, int votNum, int precint)
    {
        this.name = name;
        this.votNum = votNum;
        this.precint = precint;
    }
    

    【讨论】:

      【解决方案3】:

      为你的 voter 类中的所有方法添加一个返回类型。

      目前在您的代码中,您只显示了一种方法showDetails(),它没有返回类型。当然还有其他方法你还没有声明返回类型。

      【讨论】:

        【解决方案4】:

        invalid method declaration; return type required

        错误信息说得很清楚;您需要给出每种方法的返回类型。如果没有返回类型,就给 void。

        【讨论】:

          【解决方案5】:

          方法应该有返回类型,说明返回值的类型(如果从方法返回任何内容)。

          如果没有返回任何内容,则指定 void。

          这正是您的 setDetails 方法中缺少的内容。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-02-21
            • 1970-01-01
            • 1970-01-01
            • 2017-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多