对象 课堂在教室里 JAVA朗 包裹Java中的每个类都直接或间接地从 对象 类,从今以后它是对象类的子类。如果一个类不扩展任何其他类,那么它就是 对象 如果扩展了另一个类,那么它是间接派生的。因此,所有Java类都可以使用对象类方法。
注: 对象类在任何java程序中充当继承层次结构的根
现在我们将讨论它的一个方法,称为toString()方法。我们通常使用toString()方法来获取对象的字符串表示形式。这一点非常重要,读者应该知道,每当我们试图打印对象引用时,就会在内部调用toString()方法。如果我们没有在类中定义toString()方法,那么将调用对象类toString()方法,否则将调用我们实现的或重写的toString()方法。
语法:
public String toString() { return getClass().getName()+"@"+Integer.toHexString(hashCode());}
注: 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 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()); } } |
Best_Friend@3d075dc0Best_Friend@3d075dc0
输出说明: 在上面的程序中,我们创建了一个Best_Friend类的对象,并提供了一个朋友的所有信息。但是当我们试图打印对象时,我们得到的输出是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 KiriburuGulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
注: 在所有包装器类中,所有集合类、字符串类、StringBuffer、StringBuilder类和toString()方法都会被覆盖,以实现有意义的字符串表示。因此,强烈建议在我们的类中重写toString()方法。
例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@232204a1Gulpreet Kaur21[BIT, M.TECH]
本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。