【问题标题】:How to call a method twice in a row?如何连续两次调用一个方法?
【发布时间】:2023-04-01 08:04:01
【问题描述】:

我有一个代码,在我的课堂上,我希望能够像qns.answer(5).answer(7) 一样两次调用方法答案。但是现在当我如下调用方法答案时,我得到错误找不到符号。

例如:

Question qns = new Question("How many apples are there in the bag?")
qns ==> How many apples are there in the bag?: My answer is 0.

qns.answer(12)
==> How many apples are there in the bag?: My answer is 12.

qns.answer(12).answer(4)
==> How many apples are there in the bag?: My answer is 4.

qns
qns ==> How many apples are there in the bag?: My answer is 0.
class Question {
    private final String question;
    private final int correctAns;

    public Question(String question, int correctAns) {
        this.question = question;
        this.correctAns = correctAns
    }

    public String answer(int myAns) {
        return String.format("%s: My answer is %d.", this.question, myAns);
    }

    @Override
    public String toString() {
        return String.format("%s: My answer is 0.", this.question);
    }
}

如果您能就如何解决这个问题提供一些指示,我们将不胜感激。

【问题讨论】:

  • 你指的是方法改变。你需要你的 answer 方法来返回一个问题对象
  • 但是你最终想要的无法实现,因为第一种方法总是会打印它的输出,这意味着你将有两个打印语句和两个输出。

标签: java class methods


【解决方案1】:

Question 可以有一个额外的字段来存储答案,您也可以编写一个新的构造函数来初始化该字段。

private final int currentAns;

public Question(String question, int correctAns, int currentAns) {
    this.question = question;
    this.correctAns = correctAns;
    this.currentAns = currentAns;
}

public Question(String question, int correctAns) {
    this(question, correctAns, 0);
}

// toString can now use currentAns!
@Override
public String toString() {
    return String.format("%s: My answer is %d.", this.question, currentAns);
}

然后在answer方法中,可以返回一个新的Question,指定的答案为currentAns

public Question answer(int myAns) {
    return new Question(question, correctAns, myAns);
}

现在您可以链接多个ans 调用。如果toString在其末尾被调用(无论是隐式还是显式),都可以得到想要的字符串。

JShell 中的示例:

jshell> Question qns = new Question("How many apples are there in the bag?", 1);

qns ==> How many apples are there in the bag?: My answer is 0.

jshell> qns.answer(12);
$7 ==> How many apples are there in the bag?: My answer is 12.

jshell> qns.answer(12).answer(4);
$8 ==> How many apples are there in the bag?: My answer is 4.

jshell> qns
qns ==> How many apples are there in the bag?: My answer is 0.

【讨论】:

  • 这正是我想要的,我现在可以调用两次 answer 方法。谢谢!
【解决方案2】:

如果你想在一个对象上多次调用相同或不同的方法(=链接),你需要在方法中返回this

既然你试图实现的目标真的不清楚,我会提出一些建议:

class Question {
    private final String question;
    private final int correctAns;

    private static String format(String quest, int ans) {
        return String.format("%s: My answer is %d.", quest, ans);
    }

    public Question(String question) {
        this.question = question;
        this.correctAns = 0;
    }

    public String answer(int myAns) {
        this.correctAns = myAns;
        System.out.println("" + this);
        return this;
    }

    @Override
    public String toString() {
        return format(this.question, this.correctAns);
    }
}

这样您可以在链中调用 answer(),因为它返回对象本身 (this)。

【讨论】:

  • 上述实现会将默认答案 0 更改为新答案,这不是我想要的。不过感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多