简体   繁体   中英

java: cannot find symbol; symbol: variable length

I apologize if this is too beginner of a question. I understand that some how my [ Object o ] is out of scope, that is why it cannot find the symbol.

However, I can't seem to fix the problem. I'm completely unsure of where it went wrong. I believe this is the last problem in my Assignment, and it's due midnight.

Any help would be greatly appreciated :)

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;

^ thats where the problem is

You need to cast your Object into a Square :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM