Home  >  Article  >  Java  >  Code example for overriding tostring method in Java

Code example for overriding tostring method in Java

黄舟
黄舟Original
2017-10-12 10:41:562115browse

This article mainly introduces the java tostring method rewriting code example, which has certain reference value. Friends who need it can learn about it.

When an object needs to be output to the display, its toString() method is usually called to convert the object's content into a string. All classes in Java have a toString() method by default

By default, System.out.println (object name) or System.out.println (object name.toString()) outputs the class name of this object and the first address of the memory corresponding to this object. If you want Custom output information must override the toString() method

Notes

1. Must be declared as public

2. The return type is String

3. The name of the method must be toString and has no parameters

4. Do not use the output method System in the method body. out.println()


import java.util.*; 
public class TreeSetTest { 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    SortedSet parts=new TreeSet(); 
    parts.add(new Item("Toaster", 1234)); 
    parts.add(new Item("Widget", 4562)); 
    parts.add(new Item("Modem", 9912)); 
    System.out.println(parts); 
    SortedSet sortByDescription=new TreeSet(new  
        Comparator() 
        { 
          public int compare(Item a, Item b) 
          { 
            String descrA=a.getDescription(); 
            String descrB=b.getDescription(); 
            return descrA.compareTo(descrB); 
          } 
        }); 
    sortByDescription.addAll(parts); 
    System.out.println(sortByDescription); 
  } 
} 
class Item implements Comparable 
{ 
  public Item(String aDescription, int aPartNumber) 
  { 
    description=aDescription; 
    partNumber=aPartNumber; 
  } 
  public String getDescription() 
  { 
    return description; 
  } 
  public boolean equals(Object otherObject) 
  { 
    if(this==otherObject) 
      return true; 
    if(otherObject==null) 
    { 
      return false; 
    } 
    if (getClass()!=otherObject.getClass()) 
    { 
      return false; 
    } 
    Item other=(Item)otherObject; 
    return description.equals(other.description)&& 
        partNumber==other.partNumber; 
  } 
  public int hashCode() 
  { 
    return 13*description.hashCode()+17*partNumber; 
  } 
  public int compareTo(Item other) 
  { 
    return partNumber-other.partNumber; 
  } 
  private String description; 
  private int partNumber; 
}

The output is:


[Item@8c9e3a56, Item@d780c206, Item@39c021ba]
[Item@39c021ba, Item@8c9e3a56, Item@d780c206]

ItemOverloaded toString ()After method :


import java.util.*; 
public class TreeSetTest { 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    SortedSet parts=new TreeSet(); 
    parts.add(new Item("Toaster", 1234)); 
    parts.add(new Item("Widget", 4562)); 
    parts.add(new Item("Modem", 9912)); 
    System.out.println(parts); 
    SortedSet sortByDescription=new TreeSet(new  
        Comparator() 
        { 
          public int compare(Item a, Item b) 
          { 
            String descrA=a.getDescription(); 
            String descrB=b.getDescription(); 
            return descrA.compareTo(descrB); 
          } 
        }); 
    sortByDescription.addAll(parts); 
    System.out.println(sortByDescription); 
  } 
} 
class Item implements Comparable 
{ 
  public Item(String aDescription, int aPartNumber) 
  { 
    description=aDescription; 
    partNumber=aPartNumber; 
  } 
  public String getDescription() 
  { 
    return description; 
  } 
  public String toString() 
  { 
    return "[description="+description 
        +",partNumber="+partNumber+"]"; 
  } 
  public boolean equals(Object otherObject) 
  { 
    if(this==otherObject) 
      return true; 
    if(otherObject==null) 
    { 
      return false; 
    } 
    if (getClass()!=otherObject.getClass()) 
    { 
      return false; 
    } 
    Item other=(Item)otherObject; 
    return description.equals(other.description)&& 
        partNumber==other.partNumber; 
  } 
  public int hashCode() 
  { 
    return 13*description.hashCode()+17*partNumber; 
  } 
  public int compareTo(Item other) 
  { 
    return partNumber-other.partNumber; 
  } 
  private String description; 
  private int partNumber; 
}

The output is:


[[description=Toaster,partNumber=1234], [description=Widget,partNumber=4562], [description=Modem,partNumber=9912]]
[[description=Modem,partNumber=9912], [description=Toaster,partNumber=1234], [description=Widget,partNumber=4562]]

Summary

The above is the detailed content of Code example for overriding tostring method in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn