1
我运行下面的我如何比较对象与另一个对象
Choice choice1 = new Choice(0);
Choice choice2 = new Choice(1);
int result = choice1.compareWith(choice2);
IO.outputln("Actual: " + result);
的compareWith方法
public int compareWith(Choice anotherChoice)
{
int result=0;
if (anotherChoice==0||type==0)
result=1;
if (anotherChoice==1&&type==1)
result=-11;
}
节目中说,我不能比较一个整数anotherchoice(选择类)。我该怎么做。
的错误说,anotherchoice的类型为'Choice'。我假设你想比较实例变量'type'。所以它应该是'anotherChoice.type == this.type' – sidgate