简体   繁体   English

我怎样才能在课程之间进行更改?

[英]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.除了修改 main 方法。 Revise only A method.只修改A方法。 I tried to make change the Object a2 to an a2.我试图将 Object a2 更改为 a2。 How Can I change that in the A class?如何在 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. 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:在实现.equals方法时,您应始终先执行以下三项检查,以确保 object 的安全,然后再尝试比较其属性:

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.如果两个对象是完全相同的object,意味着它们是同一个实例,而不仅仅是两个具有相同属性的对象,则立即返回true,因为不需要检查属性。

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 )这可以通过尝试访问null object 的属性来防止NullPointerException (例如在您的代码中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.如果 object 不是 class 的实例,则类型转换将失败(就像在您的代码中所做的那样),这可以通过在尝试转换之前返回false来防止这种情况发生。

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请记住,如果您要比较的属性不是原始属性,则应在它们上使用.equals

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.您的代码失败的原因是当您将 Object ( a2 ) 的实例传递给 equals 方法时,您试图将其转换为A类型的引用。 But since a2 is actually a pure instance of Object and not of A , this cast fails.但由于a2实际上是Object而不是A的纯实例,因此此转换失败。 Even though Object is the baseclass for everything in Java, including your self defined A , you are casting in the wrong direction.尽管Object是 Java 中所有内容的基类,包括您自定义的 A ,但您的方向是错误的。 An Object does not hold an attribute x so casting this way would be unsafe. Object 不包含属性x因此以这种方式进行转换是不安全的。 Java's typechecking mechanism catches this and throws an error when you try to cast. Java 的类型检查机制会捕捉到这一点,并在您尝试强制转换时抛出错误。

Have a look at a document explaining inheritance and casting to get the basics of this.查看解释 inheritance 和铸造的文档以了解此基础知识。 Eg, this one .例如, 这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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