Java中的对象 toString() 方法
对象类存在于Java.lang包中。 Java中的每个类都直接或间接地派生自Object类,因此它是 Object 类的子类。如果一个类不扩展任何其他类,则它是Object的直接子类,如果扩展另一个类,则它是间接派生的。因此,Object 类方法可用于所有Java类。
Note: Object class acts as a root of inheritance hierarchy in any java program
现在我们将处理它的一种方法,称为 toString() 方法。我们通常确实使用 toString() 方法来获取对象的字符串表示形式。这非常重要,读者应该知道,每当我们尝试打印对象引用时,都会在内部调用 toString() 方法。如果我们没有在您的类中定义 toString() 方法,则调用 Object 类的 toString() 方法,否则将调用我们实现或覆盖的 toString() 方法。
句法:
public String toString() {
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
Note: Default behavior of toString() is to print class name, then @, then unsigned hexadecimal representation of the hash code of the object.
例子
JAVA
// Java program to Illustrate
// working of toString() method
// Main class
class Best_Friend {
// Member attributes of this class
String name;
int age;
String college;
String course;
String address;
// Constructor of this class
Best_Friend(String name, int age, String college,
String course, String address)
{
// This variable refers to current instance itself
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
// Method of this class
// Main driver method
public static void main(String[] args)
{
// Creating an object of this class
// Custom attributes been passed as in arguments
Best_Friend b = new Best_Friend(
"Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
"Kiriburu");
// Print and display commands to illustrate
// toString() method as both will print the same
// Print the object
System.out.println(b);
// Print the object but implicitly using toString()
// method
System.out.println(b.toString());
}
}
JAVA
// Java program to illustrate
// working of toString() method
// Main class
class Best_Friend {
// Member attributes of this class
String name;
int age;
String college;
String course;
String address;
// Constructor of this class
Best_Friend(String name, int age, String college,
String course, String address)
{
// This keyword refers to current instance itself
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
// Method 1
// Creating our own toString() method
public String toString()
{
return name + " " + age + " " + college + " "
+ course + " " + address;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating object of class inside main() method
Best_Friend b = new Best_Friend(
"Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
"Kiriburu");
// Print and display commands to illustrate
// toString() method as both will print the same
// Print the object
System.out.println(b);
// Printing object but using toString() method
System.out.println(b.toString());
}
}
JAVA
// Java program to illustrate
// working of toString() method
// Importing all utility classes
import java.util.*;
// Main class
class Best_Friend {
// Member attributes of this class
String name;
int age;
String college;
String course;
String address;
// Constructor of this class
Best_Friend(String name, int age, String college,
String course, String address)
{
// This keyword refer to current instance itself
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
// Method of this class
public static void main(String[] args)
{
// Creating an object of class in main() method
Best_Friend b = new Best_Friend(
"Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
"Kiriburu");
System.out.println(b);
String s = new String("Gulpreet Kaur");
System.out.println(s);
Integer i = new Integer(21);
System.out.println(i);
ArrayList l = new ArrayList();
l.add("BIT");
l.add("M.TECH");
System.out.println(l);
}
}
Best_Friend@3d075dc0
Best_Friend@3d075dc0
输出说明:在上面的程序中,我们创建了一个 Best_Friend 类的 Object,并提供了一个朋友的所有信息。但是当我们尝试打印对象时,我们会得到一些格式为 classname@HashCode_in_Hexadeciaml_form 的输出。如果我们想要关于 Best_friend 对象的正确信息,那么我们必须在 Best_Friend 类中重写 Object 类的 toString() 方法。
示例 2:
Java
// Java program to illustrate
// working of toString() method
// Main class
class Best_Friend {
// Member attributes of this class
String name;
int age;
String college;
String course;
String address;
// Constructor of this class
Best_Friend(String name, int age, String college,
String course, String address)
{
// This keyword refers to current instance itself
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
// Method 1
// Creating our own toString() method
public String toString()
{
return name + " " + age + " " + college + " "
+ course + " " + address;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating object of class inside main() method
Best_Friend b = new Best_Friend(
"Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
"Kiriburu");
// Print and display commands to illustrate
// toString() method as both will print the same
// Print the object
System.out.println(b);
// Printing object but using toString() method
System.out.println(b.toString());
}
}
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Note: In all wrapper classes, all collection classes, String class, StringBuffer, StringBuilder classes toString() method is overriden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.
示例 3:
Java
// Java program to illustrate
// working of toString() method
// Importing all utility classes
import java.util.*;
// Main class
class Best_Friend {
// Member attributes of this class
String name;
int age;
String college;
String course;
String address;
// Constructor of this class
Best_Friend(String name, int age, String college,
String course, String address)
{
// This keyword refer to current instance itself
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
// Method of this class
public static void main(String[] args)
{
// Creating an object of class in main() method
Best_Friend b = new Best_Friend(
"Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
"Kiriburu");
System.out.println(b);
String s = new String("Gulpreet Kaur");
System.out.println(s);
Integer i = new Integer(21);
System.out.println(i);
ArrayList l = new ArrayList();
l.add("BIT");
l.add("M.TECH");
System.out.println(l);
}
}
输出:它也会抛出未检查和不安全操作的警告
Best_Friend@232204a1
Gulpreet Kaur
21
[BIT, M.TECH]