如何连续调用一个方法两次?
How to call a method twice in a row?
我有一个代码,在我的 class 中,我希望能够像 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);
}
}
如果您能就如何解决这个问题提供一些指导,我们将不胜感激。
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
方法中,您可以 return 一个新的 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);
==> How many apples are there in the bag?: My answer is 12.
jshell> qns.answer(12).answer(4);
==> 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.
如果你想在一个对象上多次调用相同或不同的方法(=chaining),你需要return this
in方法。
既然你试图实现的目标真的不清楚,我会提出一些建议:
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(),因为它 return 是对象本身 (this
)。
我有一个代码,在我的 class 中,我希望能够像 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);
}
}
如果您能就如何解决这个问题提供一些指导,我们将不胜感激。
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
方法中,您可以 return 一个新的 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);
==> How many apples are there in the bag?: My answer is 12.
jshell> qns.answer(12).answer(4);
==> 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.
如果你想在一个对象上多次调用相同或不同的方法(=chaining),你需要return this
in方法。
既然你试图实现的目标真的不清楚,我会提出一些建议:
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(),因为它 return 是对象本身 (this
)。