Problem med getters and setters i Java
Så jag har 3 klasser, Contact, PhoneNumber och Contact Test. När jag kör ConctacTest så gör den en instans av Contact som i sin tur ger vissa värden till konstruktorn i PhoneNumber. Den ska sedan returnera det till Contact som sedan returnerar det hela. Jag lägger upp allihopa här:
PhoneNumber:
public class PhoneNumber {
private String home;
private String work;
private String mobile;
public PhoneNumber(String home, String work, String mobile){
this.home = home;
this.work = work;
this.mobile = mobile;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String toString(){
return "Home: " + home + "Work: " + work + "Mobile: " + mobile;
}
Contact:
public class Contact {
private String name;
private PhoneNumber phone;
public Contact(String name, String home, String work, String mobile){
this.name=name;
phone.setHome(home);
phone.setWork(work);
phone.setMobile(mobile);
}
public String getName(){
return name;
}
public String getHome(){
return phone.getHome();
}
public String getWork(){
return phone.getWork();
}
public String getMobile(){
return phone.getMobile();
}
public String toString(){
return name + phone.toString();
}
}
ContactTest
public class ContactTest {
public static void main(String[] args) {
Contact contact = new Contact("Eva Bok","040-123456","040-333333","0705223344");
System.out.println( "Namn: " + contact.getName() + "\n" + "Telefonnummer: " + contact.getHome() + ", " + contact.getWork() + ", " + contact.getMobile() );
System.out.println("--------------------------------");
System.out.println( contact.toString() );
}
}
Problemet är alltså att jag inte får det att funka. När den kommer till phone.setHome(home); i konstruktorn i Contact så hittar den inte phone.setHome.