📜  Java |继承|问题9

📅  最后修改于: 2021-07-02 15:25:30             🧑  作者: Mango

final class Complex {
  
    private final double re;
    private final double im;
  
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
  
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }
}
  
class Main {
  public static void main(String args[])
  {
       Complex c = new Complex(10, 15);
       System.out.println("Complex number is " + c);
  }         
}

(一种)

Complex number is (10.0 + 15.0i)

(B)编译器错误
(C)

Complex number is SOME_GARBAGE

(D)

Complex number is Complex@8e2fb5

这里8e2fb5是c的哈希码答案: (A)
说明:参见https://www.geeksforgeeks.org/overriding-tostring-method-in-java/
这个问题的测验