首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用构造函数和访问器方法

使用构造函数和访问器方法
EN

Stack Overflow用户
提问于 2015-07-01 22:24:24
回答 1查看 622关注 0票数 0
代码语言:javascript
复制
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);
    }

}

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

代码语言:javascript
复制
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();
        }
    }

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

我刚刚开始学习java,并在本章中使用访问器和构造函数方法。任何帮助都会很棒!!谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-07-01 22:44:31

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

如果您有一个变量foo,则它的访问器通常以以下形式出现:

代码语言:javascript
复制
void setFoo(<type> value) {
    foo = value
}

<type> getFoo() {
    return foo;
}

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

构造函数是声明的方法,其名称与包含在其中的类的名称相同,而声明中没有返回类型。例如,对于您可能拥有的MatchScore

代码语言:javascript
复制
public MatchScore(String num, String kills, String death) {
    MatchNumber = num;
    KillsInMatch = kills;
    DeathsInMatch = death;
}

(顺便说一句,字段和局部变量通常都是非大写的,所以它们应该是matchNumberkillsInMatchdeathsInMatch )。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31172703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档