【问题标题】:java: cannot find symbol; symbol: variable lengthjava: 找不到符号;符号:可变长度
【发布时间】:2017-02-24 11:20:23
【问题描述】:

如果这个问题太初学者了,我深表歉意。我知道我的 [ 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

错误:(24, 22) java: 找不到符号 符号:可变长度 位置:java.lang.Object 类型的变量 o

if (o.*length* == this.length)
            return true;

^这就是问题所在

【问题讨论】:

  • Objects 没有字段 length。投射物体并检查。
  • 所以任何长度为 50 倍数的正方形都将被视为==?至少,您的哈希码实现是这样说的

标签: java


【解决方案1】:

您需要将Object 转换为Square

Square otherSquare = (Square) o;
...
if (otherSquare.length == this.length)
    return true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多