当我尝试System.out.println时

时间:2015-10-19 17:40:49

标签: java

我创建了一个家庭作业项目程序,允许我输入所有数据一旦运行EG:名字等等所以我的公共课如下:

public Borrower(String fName, String lName, String lNumber, int numberOfBooks,
        String street, String town, String postcode) {  
  firstName = fName;  
  lastName = lName;  
  libraryNumber = lNumber;  
  noOfBooks = numberOfBooks;   
  address = new Address(street, town, postcode);  
}  

然后我有一节如:

public void printBorrowerDetails() {  
  System.out.println(firstName + " " + lastName+ "\n" + "\n" +"Library Number: " 
      + libraryNumber +  "\n" +"Number of loans: " + noOfBooks);  
}

如何编写,Address将以此格式打印

FirstName SecondName  
street  
town  
postcode  

地址类的代码在另一个已链接的类中,代码是

public class Address {

    private String postcode;
    private String street;
    private String town;

    public Address (String street, String town, String postcode)
    { 
        this.street= street;
        this.town= town;
        this.postcode= postcode;
    }

    public String getFullAddress()
    {
        return street+"\n"+town+"\n"+postcode;
    }

    public String getPostcode()
    {
        return postcode;
    }

    public String getStreet()
    {
        return street;
    }

    public String getTown()
    {
        return town;
    }

    public void printAddress()
    {
        System.out.println(street+"\n"+town+"\n"+postcode);

    }

    public void setFullAddress(String street, String town, String postcode)
    {
        street= street;
        town= town;
        postcode= postcode;
    }

    public void setPostcode(String postcode)
    { 
        this.postcode= postcode;
    }

    public void setStreet(String street)
    {
        this.street= street;
    }

    public void setTown(String town)
    {
        this.town= town;
    }

}

6 个答案:

答案 0 :(得分:2)

假设您有地址'街道,城镇和邮政编码的getter方法以及地址的私有变量,有两种方法可以实现此目的:

vagrant up --provision

System.out.println(address.getStreet() + "\n" + address.getTown() + "\n" + getPostcode());

另外,如果您要打印出地址的详细信息,您应该尽量将其保留为地址,而不是为了良好的实践而包括借款人。如果您确实要打印System.out.println(address.getStreet()); System.out.println(address.getTown()); System.out.println(address.getPostcode()); FirstName,则需要将这些值传递给LastName构造函数,但它不再是只是 {{ 1}}。

答案 1 :(得分:0)

System.out.print(FirstName + " " + SecondName + "\n");
System.out.println(street + "\n" + town + "\n" + postcode);

你必须使用print function而不是println来完成这项工作......

答案 2 :(得分:0)

您可以使用Java API中的MessageFormat class。例如:

String result = MessageFormat.format(
    "{0} {1} \n {2} \n {3} \n {4}",
    borrower.firstName, Borrower.secondName, address.street, address.town, address.postcode);

对对象使用getter方法。

答案 3 :(得分:0)

System.out.println(address.getStreet());
System.out.println(address.getTown());
System.out.println(address.getPostcode());

答案 4 :(得分:0)

    private streetname;
    private townname;
    private postnumber;
    public void address(string street,string town,int postcode){
         streetname=street;
         townname=town;
         postnumber=post;
 System.out.println(streetname +"\n"+ townname  +"\n"+ postnumber);

}

答案 5 :(得分:0)

您可以做的另一件事是将toString()方法调用到Address类。 toString实际上是每个对象使用的方法(Object超类iirc的一部分),每当您尝试使用System.out.print打印对象时,它都会自动调用。但是,默认情况下,此方法返回哈希码,我们在您的情况下不需要。因此,您必须通过向对象类添加新的toString方法来覆盖默认值,如此。

public class Address{
    .
    .
    .
    public String toString(){
        return street + "\n" + town + "\n" + postcode;
    }
}

然后当你使用

System.out.println(address);

它将按照对象的toString()方法中的指定进行打印。 你也可以把它写成

System.out.println(address.toString());

虽然像我说的那样,但这并不是必需的,因为java知道调用toString()方法。

toString()可以节省时间,并且当您有多个要打印的对象时,可以减少混乱。

-edit -

值得指出的是,您也可以为借款人类做到这一点。

public class Borrower{
     .
     .
     .
     public String toString(){
          return fName + " " + lName + "\n\nLibrary Number: " + libraryNumber
        + "\nNumber Of Loans: " + noOfBooks + "\n" + address;
     }
}

然后在你的主要代码中,只是把...(为了演示,我们假设你把你的借款人称为“借款人”)

System.out.println(borrower);