如果我将类的实例变量与对象进行比较,它会比较什么?

huangapple go评论68阅读模式
英文:

If i compared the instance variable of class with the object what will it compare?

问题

public class Notepad {
    private String name;
    private int year;

    public Notepad(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public boolean equals(Object object) {
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }

        if (object == this) {
            return true;
        }

        Notepad compared = (Notepad) object;

        return this.name.equals(compared.name);
    }
}
Notepad basics = new Notepad("Equals basics", 2000);
Notepad advanced = new Notepad("Equals advanced", 2001);

System.out.println(basics.equals(basics));
System.out.println(basics.equals(advanced));
System.out.println(basics.equals(new Notepad("Equals basics", 2000)));
System.out.println(basics.equals(new Notepad("Equals basics", 2001)));

在上述代码中,最后两行中当我比较 basics 对象时,返回 false。在 equals 方法中,当我检查 this.name.equals(compared) 时,它实际上在比较什么?

英文:
public class Notepad {
    private String name;
    private int year;
 
    public Notepad(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public boolean equals(Object object) {
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }

        if (object == this) {
            return true;
        }

        Notepad compared = (Notepad) object;

        return this.name.equals(compared);
    }
}
Notepad basics = new Notepad("Equals basics", 2000);
Notepad advanced = new Notepad("Equals advanced", 2001);

System.out.println(basics.equals(basics));
System.out.println(basics.equals(advanced));
System.out.println(basics.equals(new Notepad("Equals basics", 2000)));
System.out.println(basics.equals(new Notepad("Equals basics", 2001)));

In the above code at the last two lines when i compare the basics object it returns false.In the equals method when i check this.name.equals(compared) what does it actually compares?

答案1

得分: 2

return this.name.equals(compared);

这里你正在比较 `NotePad` 对象的 `name` 字段与另一个 `NotePad` 对象

你应该与 `compared` 的 name 字段进行比较

return this.name.equals(compared.name);

当将 String 与 `NotePad` 对象进行比较时会发生什么

String 的 `equals` 方法文档

> 将此字符串与指定对象进行比较当且仅当参数不为 null 并且**是 String 对象**表示与此对象相同的字符序列时结果为 true

因此`NotePad` 对象进行比较不是一个 String 对象因此将返回 false
英文:
return this.name.equals(compared);

Here you are comparing name field of NotePad object with the object of NotePad.

You should compare with the name field of the compared

return this.name.equals(compared.name);

What happened when compare String with NotePad object ?

Doc for String equals method

> Compares this string to the specified object. The result is true if
> and only if the argument is not null and is a String object that
> represents the same sequence of characters as this object.

So as comparing Notpad object is not a String object it will return false.

答案2

得分: 1

this.name将不会与Notepad对象中的任何内容进行比较。与您的Notepad.equals方法类似,String.equals方法将首先检查提供的对象是否不为null,然后再检查它是否属于适当的类,即String类。Notepad不是String,因此此处比较终止。不会测试给定对象的任何成员。

英文:

this.name will not be compared to anything in the Notepad object. Similarly to your Notepad.equals method, the String.equals will check if the object supplied is not null first, and then if it is of an appropriate class, that is a String. A Notepad is not a String so here the comparison ends. No members of the given object are tested.

答案3

得分: 0

this.name返回一个字符串,它是一个对象。

你正在将this.name与类别为Notepad的对象compared进行比较。如果你想比较这些名称,请使用:

this.name.equals(compared.name)

如果你希望在相同的条件下返回true,请重写Project类的equals()hashCode()方法。

只有当两个对象的hashCode()方法返回的整数值相等时,equals()方法才会返回true。

英文:

this.name returns String which is an object.

You are comparing this.name with object compared of class Notepad. If you want to compare the names use:

this.name.equals(compared.name)

If you want to have the same condition return true, override equals() and hashCode() methods of Project class.

equals() returns true only if int value returned by hashCode()method for both the objects is equal.

答案4

得分: 0

使用getClass()方法来比较对象...
我希望这段代码能对你有所帮助...

public class Notepad {

    private String name;
    private int year;

    public Notepad(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public boolean equals(Object object) {
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }

        if (object.getClass() == this.getClass()) {
            return true;
        }

        Notepad compared = (Notepad) object;

        return object.equals(compared);
    }

    public static void main(String[] args) {
        Notepad basics = new Notepad("Equals basics", 2000);
        Notepad advanced = new Notepad("Equals advanced", 2001);

        System.out.println(basics.equals(basics));
        System.out.println(basics.equals(advanced));
        System.out.println(basics.equals(new Notepad("Equals basics", 2000)));
        System.out.println(basics.equals(new Notepad("Equals basics", 2001)));
    }
}
英文:

Use getClass() method to compare Objects...
I hope this code will help you...

public class Notepad {

    private String name;
    private int year;

    public Notepad(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public boolean equals(Object object) {
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }

        if (object.getClass() == this.getClass()) {
            return true;
        }

        Notepad compared = (Notepad) object;

        return object.equals(compared);
    }

    public static void main(String[] args) {
        Notepad basics = new Notepad("Equals basics", 2000);
        Notepad advanced = new Notepad("Equals advanced", 2001);

        System.out.println(basics.equals(basics));
        System.out.println(basics.equals(advanced));
        System.out.println(basics.equals(new Notepad("Equals basics", 2000)));
        System.out.println(basics.equals(new Notepad("Equals basics", 2001)));
    }
}

huangapple
  • 本文由 发表于 2020年7月26日 14:16:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定