简体   繁体   中英

How can I change between classes?

This is what I wrote.

public class Test {
    public static void main(String[] args) {
        Object a1 = new A();
        Object a2 = new Object();

        System.out.println(a1.toString());

        System.out.println((a1 == a2) + " " + (a1.equals(a2)));
    }
}

class A {
    int x;

    public boolean equals(Object obj) {
        A _obj = (A) obj;
        return x == _obj.x;
    }

    public String toString() {
        return "A's x is " + x;
    }
}

How can I make 'false true' on the console? Except revising the main method. Revise only A method. I tried to make change the Object a2 to an a2. How Can I change that in the A class?

The reason you're getting the error class java.lang.Object cannot be cast to class A is because the object you're comparing it to is not an instance of class A, so trying to cast the object as such will fail.

When implementing the .equals method, you should always perform these three checks first to ensure the safety of the object before you try comparing its properties:

if (obj == this) return true; If the two objects are the exact same object, meaning that they are the same instance, not just two objects with the same properties, immediately return true because there is no need to check the properties.

if (obj == null) return false; This prevents a NullPointerException by trying to access a property of a null object (such as when in your code you do return x == _obj.x )

if (;(obj instanceof A)) return false; If the object is not an instance of your class, the typecast will fail (as it did in your code) and this protects against that by returning false before trying to cast.

Finally, if the code reaches this point you can cast and compare the objects as you had done in your code:

A _obj = (A) obj;
return this.x == _obj.x;

Keep in mind that if the properties you are comparing are not primitives, you should use .equals on them

First of all, what do you mean by "making false true" exactly? I assume you want your code to run, but could you give us a bit more context of what you are trying to do?

The reason your code fails is that you are trying to cast your instance of an Object ( a2 ) onto a reference of type A when you pass it into the equals method. But since a2 is actually a pure instance of Object and not of A , this cast fails. Even though Object is the baseclass for everything in Java, including your self defined A , you are casting in the wrong direction. An Object does not hold an attribute x so casting this way would be unsafe. Java's typechecking mechanism catches this and throws an error when you try to cast.

Have a look at a document explaining inheritance and casting to get the basics of this. Eg, this one .

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