📜  在Java中重写 toString() 方法

📅  最后修改于: 2022-05-13 01:55:04.930000             🧑  作者: Mango

在Java中重写 toString() 方法

Java是面向对象的,只处理类和对象,所以如果我们确实需要任何计算,我们会使用与类对应的对象的帮助。它是Java最常用于获取对象的字符串表示的方法。现在您一定想知道,到目前为止,他们在使用 System.out.print 时并没有使用相同的内容,而是在控制台上获取字符串表示或简短输出。这是因为在编写打印语句时会自动调用此方法。所以这个方法被覆盖,以便返回下面通过示例展示的对象的值。

示例 1:

Java
// file name: Main.java
 
class Complex {
    private double re, im;        
 
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
}
  
// Driver class to test the Complex class
public class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(10, 15);
        System.out.println(c1);
    }
}


Java
// Java Program to illustrate Overriding
// toString() Method
 
// Class 1
public class GFG {
     
    // Main driver method
    public static void main(String[] args) {
         
        // Creating object of class1
        // inside main() method
        Complex c1 = new Complex(10, 15);
         
        // Printing the complex number
        System.out.println(c1);
    }
}
// Class 2
// Helper class
class Complex {
     
    // Attributes of a complex number
    private double re, im;
     
    // Constructor to initialize a complex number
    // Default
    // public Complex() {
    //     this.re = 0;
    //     this.im = 0;
    // }
     
    // Constructor 2: Parameterized
    public Complex(double re, double im) {
         
        // This keyword refers to
        // current complex number
        this.re = re;
        this.im = im;
    }
    // Getters
    public double getReal() {
        return this.re;
    }
    public double getImaginary() {
        return this.im ;
    }
    // Setters
    public void setReal(double re) {
        this.re = re;
    }
    public void setImaginary(double im) {
        this.im = im;
    }
    // Overriding toString() method of String class
    @Override
    public String toString() {
        return this.re + " + " + this.im + "i";
    }
}


输出
Complex@214c265e

输出说明:输出是类名,然后是'at'符号,最后对象hashCode 。 Java中的所有类都直接或间接地从 Object 类继承(参见第 1 点)。 Object 类有一些基本方法,如 clone()、toString()、equals() 等。Object 中默认的 toString() 方法打印“类名@哈希码”。我们可以覆盖类中的 toString() 方法来打印正确的输出。例如,在下面的代码中 toString() 被覆盖以打印“Real + i Imag”形式。

示例 2:

Java

// Java Program to illustrate Overriding
// toString() Method
 
// Class 1
public class GFG {
     
    // Main driver method
    public static void main(String[] args) {
         
        // Creating object of class1
        // inside main() method
        Complex c1 = new Complex(10, 15);
         
        // Printing the complex number
        System.out.println(c1);
    }
}
// Class 2
// Helper class
class Complex {
     
    // Attributes of a complex number
    private double re, im;
     
    // Constructor to initialize a complex number
    // Default
    // public Complex() {
    //     this.re = 0;
    //     this.im = 0;
    // }
     
    // Constructor 2: Parameterized
    public Complex(double re, double im) {
         
        // This keyword refers to
        // current complex number
        this.re = re;
        this.im = im;
    }
    // Getters
    public double getReal() {
        return this.re;
    }
    public double getImaginary() {
        return this.im ;
    }
    // Setters
    public void setReal(double re) {
        this.re = re;
    }
    public void setImaginary(double im) {
        this.im = im;
    }
    // Overriding toString() method of String class
    @Override
    public String toString() {
        return this.re + " + " + this.im + "i";
    }
}


输出
10.0 + 15.0i