0

这是 Java,使用 BlueJ。

我有四个类,分别是 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());
}
}
4

2 回答 2

6

getTo()您的 Letter 构造函数正在调用方法,例如getFrom()在这些字段被填充之前。不要这样做,因为您的 finishedLetter String 永远不会正确"finished"。IE,

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

总会导致null + "\n" + null + "\n" + null

也许这种代码应该在toString()方法中。

于 2012-10-19T02:43:36.217 回答
1

当您的字母使用 构造时,它会使用多个值new Letter()初始化其实例字段。因为、和尚未使用其对应的 setter 设置,所以它们的 getter 返回 null,导致等于“null \nnull \nnull”。finishedLetternulltofrombodyfinishedLetter

为了解决这个问题,我的一种方法是finishedLettertoString()方法本身中定义。这既可以解决问题,又可以对程序设计采取更加面向对象的方法。

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

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

更好的方法是在构造函数中要求to、、frombody作为参数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();
}
于 2012-10-19T02:47:38.013 回答