Java中的每个类都是直接或间接成为Object类的子级。对象类包含toString()方法。我们可以使用toString()方法来获取对象的字符串表示形式。每当我们尝试打印对象引用时,就会在内部调用toString()方法。如果我们没有在您的类中定义toString()方法,则将调用对象类toString()方法,否则将调用我们的已实现/重写的toString()方法。
对象类toString()方法的语法:
public String toString() { return getClass().getName()+"@"+Integer.toHexString(hashCode()); }
// Java program to illustrate
// working of toString() method
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public static void main(String[] args)
{
Best_Friend b =
new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
System.out.println(b);
System.out.println(b.toString());
}
}
输出:
Best_Friend@232204a1 Best_Friend@232204a1
说明:在上面的程序中,我们创建一个Best_Friend类的对象,并提供朋友的所有信息。但是,当我们尝试打印对象时,我们会得到一些输出,输出形式为classname @ HashCode_in_Hexadeciaml_form。如果我们想要有关Best_friend对象的正确信息,则必须在Best_Friend类中重写Object类的toString()方法。
// Java program to illustrate
// working of toString() method
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public String toString()
{
return name + " " + age + " " + college + " " + course + " " + address;
}
public static void main(String[] args)
{
Best_Friend b =
new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
System.out.println(b);
System.out.println(b.toString());
}
}
输出:
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
注意:在所有包装器类中,所有集合类,String类,StringBuffer,StringBuilder类toString()方法均被覆盖以表示有意义的String表示形式。因此,强烈建议在我们的类中也重写toString()方法。
// Java program to illustrate
// working of toString() method
import java.util.*;
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public static void main(String[] args)
{
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]
相关用法
- Java Collator compare(Object, Object)用法及代码示例
- Java ParsePosition toString()用法及代码示例
- Java AbstractSet toString()用法及代码示例
- Java Vector toString()用法及代码示例
- Java ArrayBlockingQueue toString()用法及代码示例
- Java MathContext toString()用法及代码示例
- Java DecimalStyle toString()用法及代码示例
- Java Stack toString()用法及代码示例
- Java CopyOnWriteArrayList toString()用法及代码示例
- Java FieldPosition toString()用法及代码示例
- Java TreeSet toString()用法及代码示例
- Java Hashtable toString()用法及代码示例
- Java BigInteger toString()用法及代码示例
- Java HashSet toString()用法及代码示例
注:本文由纯净天空筛选整理自 Object toString() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。