简体   繁体   中英

How do I override an object that is being passed into a method to string?

It's exactly as the title says, I am trying to pass an object into a method, so that I can compare strings. The object doesn't convert properly, showing up as "MediaItem@9a2", and I know I have to use override but I'm not quite sure how to do this.

public class MediaItem {                                                
private String title;                                               
                                            
public String getTitle() {                                              
    return title;                                               
}                                               
                                            
public void setTitle(String title) {                                                
    this.title = title;                                             
}                                               
public MediaItem(String t)                                              
{                                               
    setTitle(t);                                                
}                                               
                                            
public MediaItem()                                              
{                                               
                                            
}                                               
                                            
public boolean equals(Object obj)                                               
{                                               
String temptitle = getTitle();                                              
temptitle = temptitle.toLowerCase();                                                
temptitle = temptitle.trim();                                               
//    System.out.println(temptitle);                                                
                                            
                                            
String tempobj = obj.toString();                                                
System.out.println(tempobj);                                                
tempobj=tempobj.toLowerCase();                                              
tempobj= tempobj.trim();                                                
System.out.println(tempobj);                                                
                                            
                                            
if (temptitle.equals(tempobj))                                              
{                                               
System.out.println("this");                                             
return true;                                                
}                                               
else{                                               
return false;                                               
}                                               
                                            
}                                               
                                            
@Override                                               
public String toString()                                                
{                                               
return obj.title;                                               
}                                               
                                            
}

There's a bad attempt at overriding at the bottom of the code. Also, not sure why all the colour has disappeared from the code.

Edit: Ok maybe I should add some more explanation: The task is to compare the obj sent in and the title of the current object. If they match, I return true. If not, I return false. The only thing I am allowed to change is the stuff within the equals(object obj) method. I can also add override. That's it. Is there an easier way to do this than what I was trying to do?

Overriding the toString() method is when you wanna do some custom external serialization for the toString() rather than the usual object hashed code. For comparing obj using equal() , you need to custom the equals method depends on your needs, like fields comparision, hashed comparison, etc.

public String toString()                                                
{                                               
     return title;                                               
}                   

is sufficient you could also use return this.title; however the this is implicit as there are no other title defined in the method

To get human readable representation of the object you should override toString() method, that's correct. However you should do it like that:

public String toString() {
   return title; // No obj!
}
// or like that
public String toString() {
 return "MyClass{title=" + title + "}";
}

What you are doing wrong is comparing objects. It's a complex topic but there is a quick example:

@Override
public boolean equals(Object o) {
    return o != null
        && getClass() == o.getClass()
        && Objects.equals(title.toLowerCase().trim(),
            o.title.toLowerCase().trim()); // for each field
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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