【问题标题】:working with constructor and accessor method使用构造函数和访问器方法
【发布时间】:2015-07-01 22:24:24
【问题描述】:
package Learning;

public class MatchScore {

    private String MatchNumber;
    private String KillsInMatch;
    private String DeathsInMatch;

    public void setMatchNumber(String nameIn){
        MatchNumber = nameIn;
    }
    public String getName(){
        return MatchNumber ;
    }

    public void setKillsInMatch(String killsIn){
        KillsInMatch = killsIn;
    }
    public String getKillsInMatch(){
        return KillsInMatch;
    }
    public void setDeathsInMatch(String deathsIn){
        DeathsInMatch = deathsIn;
    }
    public String getDeathsinMatch(){
        return DeathsInMatch;
    }

    public void totalStatus(double Stats){
        System.out.printf("This game is %s ", MatchNumber);
        System.out.printf("you killed-this many %s", KillsInMatch);
        System.out.printf("but you died-this many time %s", DeathsInMatch);
    }

}

这个 ^ 是我的构造方法。我创建了它,以便它设置匹配编号、杀死编号和死亡编号。这些只是我创建的变量,不是来自任何游戏或任何东西。

package Learning;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class MatchScoreS {
    public static void main(String args[])

            throws IOException {
                Scanner diskScanner =
                        new Scanner(new File("MatchScoress.txt")); //the file has to be in the package, in this case the Learning folder.

                for (int games = 1; games <= 5; games++) {
                    checkoutscores(diskScanner);
                        }
                    diskScanner.close();
                        }

            static void checkoutscores(Scanner aScanner) {
                MatchScore aMatch = new MatchScore();
                aMatch.setMatchNumber(aScanner.nextLine());
                aMatch.setKillsInMatch(aScanner.nextLine());
                aMatch.setDeathsInMatch(aScanner.nextLine());
                aScanner.nextLine();
        }
    }

这个 ^ 将是访问器方法。我制作了一个包含匹配号、杀戮号和死亡号的文件。我收到错误“线程“main”中的异常 java.util.NoSuchElementException:找不到行 在 java.util.Scanner.nextLine(未知来源) 在 Learning.MatchScoreS.checkoutscores(MatchScoreS.java:22) 在 Learning.MatchScoreS.main(MatchScoreS.java:15)"。 当我删除“aScanner.nextLine(); 程序并没有给我一个错误,但它也没有给我带来匹配号码等。

我刚开始学习java,我正在学习访问器和构造器方法的章节。任何帮助都是极好的!!谢谢。

【问题讨论】:

  • "这个 ^ 是我的构造方法。"不,这是整个类——它根本没有任何声明的构造函数,所以你只得到编译器默认为你声明的无参数的构造函数。但是你得到的错误基本上是因为你的文件没有足够的行......

标签: java constructor accessor


【解决方案1】:

你似乎有点困惑。您声称整个类是访问器方法,而另一个类是构造函数方法。访问器只是类中的函数,构造函数也是。

如果你有一个变量foo,它的访问器通常是这样的:

void setFoo(<type> value) {
    foo = value
}

<type> getFoo() {
    return foo;
}

因此,在您的代码中,setMatchNumber()、getName()、setKillsInMatch()、getKillsInMatch() 等是访问器(尽管 getName() 应该称为 getMatchNumber())。

构造函数是一种声明方法,它与包含它的类同名,声明中没有返回类型。例如,对于 MatchScore 类,您可能有

public MatchScore(String num, String kills, String death) {
    MatchNumber = num;
    KillsInMatch = kills;
    DeathsInMatch = death;
}

(顺便说一句,通常字段和局部变量以非大写开头,因此它们应该是matchNumber、killsInMatch 和deathsInMatch)。

现在,针对您的特定异常:您的 Matchscoress.txt 文件看起来只有 3 行,因此前三个 nextLine() 调用成功,第四个引发异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多