我正在编写一个 BigInteger 类来处理大数字。
我的 BigInteger 类有一个字符串变量 number
来保存数字的绝对值,还有一个整数变量 sign
来保存该数字的符号。
这是我比较两个数字的函数:
public boolean equals(Object other)
{
if (other instanceof BigInteger)
return toString().equals(other.toString()) && sign == other.sign;
return false;
}
但是,当我编译时,出现此错误:
java: cannot find symbol symbol: variable sign location: variable other of type java.lang.Object
有人可以修复这个错误吗?谢谢
最佳答案
您需要将 other
转换为 BigInteger
。
如果您不强制转换other
,则它被视为没有属性sign
的Object
类型(正是您遇到的错误)。
这是代码:
public boolean equals(Object other) {
if (other instanceof BigInteger) {
return toString().equals(other.toString()) && sign == ((BigInteger) other).sign;
}
return false;
}
关于java - 无法获取对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32820776/