如果这个问题太初级,我深表歉意。我知道我的 [ Object o ] 超出了范围,这就是它找不到该符号的原因。
但是,我似乎无法解决这个问题。我完全不确定哪里出了问题。我相信这是我作业中的最后一个问题,截止时间为午夜。
任何帮助将不胜感激:)
public class Square implements Shape
{
double length;
public Square(double length)
{
this.length = length;
}
@Override
public String toString()
{
return "Square";
}
@Override
public boolean equals(Object o)
{
if (o == null)
return false;
if(this.getClass() != o.getClass())
return false;
if (o.length == this.length)
return true;
else
return false;
}
@Override
public int hashCode()
{
return (int)length % 50;
}
public double getPerimeter()
{
return length * 4;
}
}
C:\Users\RimZ\IdeaProjects\Assignment1\src\Square.java
Error:(24, 22) java: cannot find symbol symbol: variable length location: variable o of type java.lang.Object
if (o.*length* == this.length)
return true;
^这就是问题所在
最佳答案
您需要将对象
转换为正方形
:
Square otherSquare = (Square) o;
...
if (otherSquare.length == this.length)
return true;
关于java: 找不到符号;符号:可变长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40053101/