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