-3
What is the function of  toString in here?
what is the need for toString

誰か toString がここで何をすべきか説明してください。私はJavaが初めてで、たくさんの新しいことを学んでいます

public class Employee  
    {
       private String name;
       private String address;
       private int number;
       public Employee(String name, String address, int number)
       {
          System.out.println("Constructing an Employee");
          this.name = name;
          this.address = address;
          this.number = number;
       }




     public String toString() //what is  this function doing
       {
          return name + " " + address + " " + number;
       }

見出し

4

3 に答える 3

0

オブジェクトの値を返す Object クラスのメソッド。

Java Docs によると:

public String toString()

オブジェクトの文字列表現を返します。一般に、toString メソッドは、このオブジェクトを「テキストで表す」文字列を返します。結果は、人が読みやすい簡潔で有益な表現でなければなりません。すべてのサブクラスでこのメソッドをオーバーライドすることをお勧めします。クラス Object の toString メソッドは、オブジェクトがインスタンスであるクラスの名前、アットマーク文字 `@'、およびオブジェクトのハッシュ コードの符号なし 16 進数表現で構成される文字列を返します。つまり、このメソッドは次の値に等しい文字列を返します。

戻り値: オブジェクトの文字列表現。

ここでJavaドキュメントを参照してください

于 2013-10-29T03:07:44.593 に答える
0

Javaドキュメントから

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', and
 * the unsigned hexadecimal representation of the hash code of the
 * object. In other words, this method returns a string equal to the
 * value of:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

于 2013-10-29T03:08:15.657 に答える