英文:
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)));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论