저는 생성자가 작동하는 방식을 이해하려고 애쓰며 두 가지 질문을 생각해 냈습니다. 저는 주소와 사람을위한 두 개의 반을 가지고 있습니다. Person 클래스에는 두 개의 Address 객체가 있습니다. 여기에 내가 뭘하는지의 간단한 예는 다음과 같습니다중첩 클래스 (Java)에서 호출되는 생성자는 언제입니까
private class Person{
private String name;
private Address unitedStates;
private Address unitedKingdom;
Person()
{
this.name = "lary"
}
Person(String n)
{
this.name = n;
//Can I call Address(string, string) here on unitedStates and unitedKingdom?
}
}//end of person class
private class Address{
private String street;
private String country;
Address()
{
this.street = "1 Washington sq";
this.country = "United States";
}
Address(String s, String c)
{
this.street = s;
this.country = c;
}
}
}
나는 사람()와 같은, 그것은 자동으로 "1 워싱턴 스퀘어"로 미국 에선 및 unitedKindom에 대한 값을 채울 것입니다두면?
그리고
은 내가 예에서 그 글을 남겼 주소 개체에 대한 인수를 전달할 수 있습니까?
아니요; 그것은 null이됩니다. – SLaks
값은 생성자가 호출 될 때 설정되지만,'Person()'에서는 절대 생성자를 호출하지 않으므로 값은'null'이됩니다. 그리고 당신은 코멘트를 남긴 생성자를 호출 할 수 있습니다. – passion