简体   繁体   English

uml图中的方法

[英]method in uml diagram

I have this uml diagram and I don't understand a part of a code that I have from my friend . 我有这个uml图,我不理解我朋友的一部分代码。 I don't understand the "equals" method.. What is used for and why should I use it ? 我不理解“等于”方法。.它的用途是什么,为什么要使用它?
This is the UML : 这是UML: 在此处输入图片说明

But I don't understand some part of the code . 但是我不理解代码的一部分。

this is a part of the code so far : 到目前为止,这是代码的一部分:

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;
}

} }

The class diagram misses that CD inherits from a general Object which provides an equals method. 类图错过了CD继承自提供equals方法的通用对象的信息。 Correctly it should look like 正确地看起来应该像

在此处输入图片说明

(My Java knowledge is near to null.) (我的Java知识几乎为空。)

Equals method is a wrapper class to perform numerical comparison.In layman terms it like comparing two number. Equals方法是用于执行数值比较的包装器类。用外行术语来说,它就像比较两个数字。 example x = 10, y =20 is x=y? 例子x = 10,y = 20是x = y? answer is no. 答案是否定的。 The same in java you have equals method which returns true if the x=y else returns false. 在Java中,您具有equals方法,如果x = y则返回true,则返回true。 In your case in the code he is overriding the method equals, to customise the comparison in the code. 在您的代码中,他将重写equals方法,以自定义代码中的比较。 He is comparing the two CD objects if they are equal it return true else it returns false. 他正在比较两个CD对象,如果它们相等,则返回true,否则返回false。

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

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