我有这个uml图,我不明白我的朋友的代码的一部分。我不明白“等于”的方法..用于什么以及为什么要使用它? 这是UML: https://developer.android.com/topic/libraries/support-library/preview/emoji-compat.html
但我不明白代码的某些部分。
到目前为止,这是代码的一部分:
class Artist {
private String name;
Artist(String name) { // constructor
this.name = name;
}
public String getName() { // name getter
return name;
}
public String toString() { // toString
return name;
}
}
class CD {
private String title;
private Artist name;
CD(String title) {
this.title = title;
}
CD(String title, Artist name) {
this.title = title;
this.name = name;
}
public Artist getArtist() {
return name;
}
public String getTitle() {
return title;
}
public String toString() {
return title + " by " + getArtist();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CD other = (CD) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}