import java.util.Arrays;
class B {
int i;
B(int i) {
this.i = i;
}
public String toString() {
return "i = " + this.i;
}
}
public class MainClass{
public static void main(String[] args) {
B [] x = new B[2];
x[0] = new B(90);
x[1] = new B(100);
B obj = new B(10);
System.out.println(obj);
System.out.println(x);//toString method of class B is not called here.
}
}
// obj를 인쇄 할 때 B 클래스의 toString 메서드가 호출되었지만 x를 인쇄하려고했을 때 아무 것도 설명하지 못했습니다 !!!배열 자바의 미묘한 동작
Arrays.toString (x) 올바른 정보가 인쇄 된 후에 알 수 있습니다.하지만 x를 인쇄했을 때 B 클래스의 toString() 메서드를 호출하지 않는 이유는 무엇입니까? – Aamir
@Aamir 더 직접적으로 대답하도록 수정되었습니다. –
고마워, 이제 알았다. !! – Aamir