如何在Java中使用toString方法并进行测试?
How to use toString method in Java and test it?
我正在尝试创建一种方法,根据电影的评级输出一定数量的星星。我正在使用 toString 方法,但不确定如何在我的测试中调用它。当我尝试编写测试时,它说 "Non-static method cannot be referenced from a static context"。我正在尝试使用 assertEquals 来查看它是否正在使用 "assertEquals("***", Rating.toString());" 输出所需的结果。我的其他方法也有同样的问题,所以我将它们设为静态,但这不是 toString 方法的一个选项。简单地将其他方法设为静态是否可以接受,或者消息是否可能暗示我的方法 and/or 测试中存在不同的缺陷(或缺陷)?如果它在代码中是一个更大的问题,这实际上可能会导致相同的答案,但是当 toString 不能是静态的时,当它给出相同的消息(无法引用非静态方法..)时,我将如何测试 toString ?
我已经查看了有关 toString 方法和测试的其他问题,但无法将其与我自己的代码相关联。另外,我不确定我是在处理一个错误还是多个错误,这使得它成为一个难以解决的问题。非常感谢您的帮助。
public class Rating {
public static int votes;
public static double score;
public static String result;
public Rating() {
votes = 0;
score = 0;
}
public static void resetVotes(){
Rating.votes = 0;
Rating.score = 0;
}
public static int getNumVotes(){
return Rating.votes;
}
public static void addVote(double rating){
Rating.votes = Rating.votes + 1;
Rating.score = Rating.score + rating;
}
public static double computeScore() {
return (Rating.score / Rating.votes);
}
public String toString() {
if (Rating.votes > 1)
{
if (computeScore() > 1 && computeScore() < 2)
{
result = "*";
}
else if (computeScore() > 2 && computeScore() < 3)
{
result = "**";
}
else if (computeScore() > 3)
{
result = "***";
}
else if (computeScore() < 1)
{
result = " ";
}
}
return result;
}
没有理由在这里有任何静态变量或方法。
可能发生的情况是您以这种方式调用测试:
AssertEquals(Rating.getNumVotes(), 5);
Rating.addVote(5.5d);
AssertEquals(Rating.getNumVotes(), 6);
AssertEquals(Rating.computeScore(), 42);
这意味着您只有一个评级,即您要修改的评级。您想要做的是创建此 class 的一个实例,它将代表一个特定的评级。所以你的测试应该是这样的:
Rating r1 = new Rating(); //An instance of Rating
r1.addVote(4.3d);
r1.addVote(4d);
r1.addVote(2.5d);
r1.addVote(5d);
Rating r2 = new Rating(); // Another instance of Rating
r2.addVote(4d);
AssertEquals(r1.getNumVotes(), 4);
AssertEquals(r2.getNumVotes(), 1);
您的评分 class 应如下所示:
我删除了static
s,并更改了toString()
方法以使其更简单。不过,请检查我获得的星星数量是否充足,因为我不确定 2.5 电影是 2 星还是 3 星。
注意到 toString()
上面的 @Override
了吗?也就是说这个方法是继承的,建议你查查。这对您的实际代码没有影响。
你看到this
关键字了吗?它指的是class的当前实例:例如,当您添加投票时,您会增加此特定评级的投票数,而不是所有评级的投票数,因为您对静态做了处理。
public class Rating {</p>
public int votes;
public double score;
public String result;
public Rating() {
votes = 0;
score = 0;
}
public void resetVotes(){
this.votes = 0;
this.score = 0;
}
public int getNumVotes(){
return this.votes;
}
public void addVote(double rating){
this.votes = this.votes + 1;
this.score = this.score + rating;
}
public double computeScore() {
return (this.score / this.votes);
}
@Override
public String toString() {
String result;
double score = computeScore();
if (this.votes > 1)
{
if (score <= 0)
result = "";
else if (score <= 1)
result = "*";
else if (score <= 2)
result = "**";
else if (score <= 3)
result = "***";
else
result = "****";
}
else
result = "No votes.";
return result;
}
}
你可以这样做:
public class yourclass {
public static String toString(bool votes, int computeScore ) {
if (votes)
{
if (computeScore > 1 && computeScore < 2)
{
result = "*";
}
else if (computeScore > 2 && computeScore < 3)
{
result = "**";
}
else if (computeScore > 3)
{
result = "***";
}
else if (computeScore < 1)
{
result = " ";
}
}
return result;
}
}
然后你可以在你的测试中这样调用它
assertEquals(yourclass.toString(true, 1), "*"))
assertEquals(yourclass.toString(true, 2), "**"))
assertEquals(yourclass.toString(true, 3), "***"))
assertEquals(yourclass.toString(true, 4), "***"))
我正在尝试创建一种方法,根据电影的评级输出一定数量的星星。我正在使用 toString 方法,但不确定如何在我的测试中调用它。当我尝试编写测试时,它说 "Non-static method cannot be referenced from a static context"。我正在尝试使用 assertEquals 来查看它是否正在使用 "assertEquals("***", Rating.toString());" 输出所需的结果。我的其他方法也有同样的问题,所以我将它们设为静态,但这不是 toString 方法的一个选项。简单地将其他方法设为静态是否可以接受,或者消息是否可能暗示我的方法 and/or 测试中存在不同的缺陷(或缺陷)?如果它在代码中是一个更大的问题,这实际上可能会导致相同的答案,但是当 toString 不能是静态的时,当它给出相同的消息(无法引用非静态方法..)时,我将如何测试 toString ?
我已经查看了有关 toString 方法和测试的其他问题,但无法将其与我自己的代码相关联。另外,我不确定我是在处理一个错误还是多个错误,这使得它成为一个难以解决的问题。非常感谢您的帮助。
public class Rating {
public static int votes;
public static double score;
public static String result;
public Rating() {
votes = 0;
score = 0;
}
public static void resetVotes(){
Rating.votes = 0;
Rating.score = 0;
}
public static int getNumVotes(){
return Rating.votes;
}
public static void addVote(double rating){
Rating.votes = Rating.votes + 1;
Rating.score = Rating.score + rating;
}
public static double computeScore() {
return (Rating.score / Rating.votes);
}
public String toString() {
if (Rating.votes > 1)
{
if (computeScore() > 1 && computeScore() < 2)
{
result = "*";
}
else if (computeScore() > 2 && computeScore() < 3)
{
result = "**";
}
else if (computeScore() > 3)
{
result = "***";
}
else if (computeScore() < 1)
{
result = " ";
}
}
return result;
}
没有理由在这里有任何静态变量或方法。
可能发生的情况是您以这种方式调用测试:
AssertEquals(Rating.getNumVotes(), 5);
Rating.addVote(5.5d);
AssertEquals(Rating.getNumVotes(), 6);
AssertEquals(Rating.computeScore(), 42);
这意味着您只有一个评级,即您要修改的评级。您想要做的是创建此 class 的一个实例,它将代表一个特定的评级。所以你的测试应该是这样的:
Rating r1 = new Rating(); //An instance of Rating
r1.addVote(4.3d);
r1.addVote(4d);
r1.addVote(2.5d);
r1.addVote(5d);
Rating r2 = new Rating(); // Another instance of Rating
r2.addVote(4d);
AssertEquals(r1.getNumVotes(), 4);
AssertEquals(r2.getNumVotes(), 1);
您的评分 class 应如下所示:
我删除了
static
s,并更改了toString()
方法以使其更简单。不过,请检查我获得的星星数量是否充足,因为我不确定 2.5 电影是 2 星还是 3 星。注意到
toString()
上面的@Override
了吗?也就是说这个方法是继承的,建议你查查。这对您的实际代码没有影响。你看到
this
关键字了吗?它指的是class的当前实例:例如,当您添加投票时,您会增加此特定评级的投票数,而不是所有评级的投票数,因为您对静态做了处理。public class Rating {</p> public int votes; public double score; public String result; public Rating() { votes = 0; score = 0; } public void resetVotes(){ this.votes = 0; this.score = 0; } public int getNumVotes(){ return this.votes; } public void addVote(double rating){ this.votes = this.votes + 1; this.score = this.score + rating; } public double computeScore() { return (this.score / this.votes); } @Override public String toString() { String result; double score = computeScore(); if (this.votes > 1) { if (score <= 0) result = ""; else if (score <= 1) result = "*"; else if (score <= 2) result = "**"; else if (score <= 3) result = "***"; else result = "****"; } else result = "No votes."; return result; } }
你可以这样做:
public class yourclass {
public static String toString(bool votes, int computeScore ) {
if (votes)
{
if (computeScore > 1 && computeScore < 2)
{
result = "*";
}
else if (computeScore > 2 && computeScore < 3)
{
result = "**";
}
else if (computeScore > 3)
{
result = "***";
}
else if (computeScore < 1)
{
result = " ";
}
}
return result;
}
}
然后你可以在你的测试中这样调用它
assertEquals(yourclass.toString(true, 1), "*"))
assertEquals(yourclass.toString(true, 2), "**"))
assertEquals(yourclass.toString(true, 3), "***"))
assertEquals(yourclass.toString(true, 4), "***"))