2012-10-19 58 views
0
返回null

這是使用BlueJ的Java。java toString()從類

我有四個類叫Person,Letter,Address和PhoneNumber。在每個中,我重寫toString()方法以返回我想要從類中獲取的值的連接字符串。當調用Letter toString()時,它將在所有值上返回null。

這個想法是使用硬編碼信息,將它傳遞到適當的類,並以標準字母格式返回。

我是否在正確的方向上打印硬編碼信息,還是應該採用不同的路線?這是一個家庭作業問題,但我覺得我碰到了一堵磚牆。

這裏是類:

public class Person 
{ 
private static String aPerson; 
private String first; 
private String middle; 
private String last;  
private Address address; 
private PhoneNumber phone; 

public String getFirst() 
{ 
    return this.first; 
} 

public void setFirst(String FirstName) 
{ 
    this.first = FirstName; 
} 

    public String getMiddle() 
{ 
    return this.middle; 
} 

public void setMiddle(String MiddleName) 
{ 
    this.middle = MiddleName; 
} 

    public String getLast() 
{ 
    return this.last; 
} 

public void setLast(String LastName) 
{ 
    this.last = LastName; 
} 

public Address getMyAddress() 
{ 
    return this.address; 
} 
public void setMyAddress(Address Address) 
{ 
    this.address = Address; 
} 

public PhoneNumber getMyPhoneNum() 
{ 
    return this.phone; 
} 

public void setMyPhoneNum(PhoneNumber Number) 
{ 
    this.phone = Number; 
} 

public Person() 
{ 
    aPerson = getFirst() + getMiddle() + getLast() + getMyAddress() + 
    getMyPhoneNum(); 
} 

public String toString() 
{ 
    return aPerson; 
} 
} 

電話號碼:

public class PhoneNumber 
{ 
private String number; 
private int areaCode = 0; 
private int phonePrefix = 0; 
private int phoneLineNum = 0; 
private int phoneExtension = 0; 

public String getNumber() 
{ 
    return number; 
} 

public void setNumber(String Number) 
{ 
    number = Number; 
} 

public int getAreaCode() 
{ 
    return areaCode; 
} 

public void setAreaCode(int AreaCode) 
{ 
    areaCode = AreaCode; 
} 

public int getPrefix() 
{ 
    return phonePrefix; 
} 

public void setPrefix(int Prefix) 
{ 
    phonePrefix = Prefix; 
} 

public int getPhoneLineNumber() 
{ 
    return phoneLineNum; 
} 

public void setLineNum(int PhoneNumber) 
{ 
    phoneLineNum = PhoneNumber; 
} 

public int getExtension() 
{ 
    return phoneExtension; 
} 

public void setExtension(int Extension) 
{ 
    phoneExtension = Extension; 
} 
} 

地址:

public class Address 
{ 
private String state; 
private String anAddress; 
private String address; 
private String city; 
private int zip = 0; 

public String getState() 
{ 
    return state; 
} 

public void setState(String State) 
{ 
    state = State; 
} 

    public String getAddress() 
{ 
    return address; 
} 

    public void setAddress(String Address) 
{ 
    address = Address; 
} 

    public String getCity() 
{ 
    return city; 
} 

    public void setCity(String City) 
{ 
    city = City; 
} 

    public int getZip() 
{ 
    return zip; 
} 

    public void setZip(int Zip) 
{ 
    zip = Zip; 
} 

public Address() 
{ 
    anAddress = getState() + getAddress() + getCity() + getZip(); 
} 

public String toString() 
{ 
    return this.anAddress; 
} 
} 

信:

public class Letter 
{ 
private Person to; 
private Person from; 
private String body; 
private String finishedLetter; 

public Person getTo() 
{ 
    return to; 
} 

public void setTo(Person newValue) 
{ 
    to = newValue; 
} 

public Person getFrom() 
{ 
    return from; 
} 

public void setFrom(Person newValue) 
{ 
    from = newValue; 
} 

public String getBody() 
{ 
    return body; 
} 

public void setBody(String newValue) 
{ 
    body = newValue; 
} 

public Letter() 
{ 
    finishedLetter = getTo() + " \n" + getFrom() + " \n" + getBody(); 
} 

public String toString() 
{ 
    return finishedLetter; 
} 
} 

而且主:

public class MainClass 
{ 
public static void main(String args[]) 
{ 
    PhoneNumber phone1 = new PhoneNumber(); 
    phone1.setAreaCode(417); 
    phone1.setPrefix(447); 
    phone1.setLineNum(7533); 
    phone1.setExtension(0); 

    PhoneNumber phone2 = new PhoneNumber(); 
    phone2.setAreaCode(210); 
    phone2.setPrefix(336); 
    phone2.setLineNum(4343); 
    phone2.setExtension(9850); 

    Address address1 = new Address(); 

    address1.setState("MO"); 
    address1.setAddress("1001 East Chestnut Expressway"); 
    address1.setCity("Springfield"); 
    address1.setZip(65807); 

    Address address2 = new Address(); 

    address2.setState("TX"); 
    address2.setAddress("4800 Calhoun Road"); 
    address2.setCity("Houston"); 
    address2.setZip(77004); 

    Person person1 = new Person(); 

    person1.setFirst("Shane"); 
    person1.setMiddle("Carroll"); 
    person1.setLast("May"); 
    person1.setMyAddress(address1); 
    person1.setMyPhoneNum(phone1); 

    Person person2 = new Person(); 

    person2.setFirst("Ted"); 
    person2.setMiddle("Anthony"); 
    person2.setLast("Nugent"); 
    person2.setMyAddress(address2); 
    person2.setMyPhoneNum(phone2); 

    Letter aLetter = new Letter(); 

    aLetter.setTo(person2); 
    aLetter.setFrom(person1); 
    aLetter.setBody("This is the body"); 

    System.out.println(aLetter.toString()); 
} 
} 
+2

您在尋求免費諮詢,因此應儘可能讓志願者幫助您。請在此發佈您的*相關*代碼。不要強迫我們通過互聯網或通過大型代碼庫來挖掘它。在此感謝您的合作。 –

+0

你有沒有至少調試過你的代碼?順便說一句,這是StackOverflow。 –

+1

與此同時,我建議編輯並將代碼從pastebin直接移至此處。只要有可能,最好在這裏提供有問題的代碼片段。如果您覺得代碼片段太長,無法在此處發佈......這意味着它可能是,並且通過關注問題來最大程度地減少需要顯示的代碼量。 –

回答

6

你的來信構造函數調用的方法,如getTo()getFrom()這些領域已經排滿了。不要這樣做,因爲您的finishedLetter字符串永遠不會正確「已完成」。即,

public Letter() 
{ 
    finishedLetter = getTo() + " \n" + getFrom() + " \n" + getBody(); 
} 

將總是導致

也許那種代碼應在toString()方法來代替。

+1

感謝您的協助。這工作完美。我現在明白你的意思是它總是返回null,因爲它沒有被正確地「完成」。 –

+0

@Joshua:不客氣。這個項目祝你好運。 –

1

當您的信件使用new Letter()構建時,它會使用多個null值初始化其實例字段finishedLetter。由於to,frombody尚未使用其相應的setter設置,因此它們的getter返回null,導致finishedLetter等於「null \ nnull \ nnull」。

要解決這個問題,我的一種方法是在toString()方法本身中定義finishedLetter。這將解決這個問題,並採取更加面向對象的方法來設計程序。

// remove constructor (if you wish) and finishedLetter field 

public String toString() { 
    return getTo() + " \n" + getFrom() + " \n" + getBody(); 
} 

一個更好的做法是,要求tofrombody,作爲Letter構造函數的參數。

// remove finishedLetter field 

public Letter(Person to, Person from, String body) { 
    this.to = to; 
    this.from = from; 
    this.body = body; 
} 

public String toString() { 
    return getTo() + " \n" + getFrom() + " \n" + getBody(); 
}