如何在 java 中的 toString() 方法中打印新行
How can I print a new line in a toString() method in java
我想在 TestBook.java 文件中写 System.out.println(b)
而不是许多 system.out.println()
行。
那么我应该在 toString()
中写什么 class 到 return 相同的结果如下所示
//Title: xxx
//Author:xxx
//Years:xxx
//Pages:xxx
//edition: xxxx
//==================================
public class Book {
String title;
String author;
int yearOfPublishing;
int numberOfPages;
int eddition;
Book ()
{}
Book ( String title, String author, int yop, int nop, int eddition)
{
this.title = title;
this.author = author;
yearOfPublishing = yop;
numberOfPages = nop;
this.eddition = eddition;
}
public String toString()
{
// return what?? how can i return new lines
}
}
public class TestBook {
public static void main(String[] args) {
Book b = new Book("Data", "Joe", 2015, 276, 3);
System.out.println ( "Title : " +b.title);
System.out.println ( "Author : " +b.author);
System.out.println ( "Year : " +b.yearOfPublishing);
System.out.println ( "Pages : " +b.numberOfPages);
System.out.println ( "Eddition : " +b.eddition);
System.out.println ("==================================");
}
}
您可以 return 换行符与您要格式化的数据内联:
return "Title : " + title + "\nAuthor : " + author ...
请注意,这可能是也可能不是解决此问题的最佳方法。
这可能是我给出的最短的答案。
Google这个:
\n
- 如果在OSX之前总是在*nix、Windows或Mac上消耗输出,则可以分别使用
\n
、\r\n
或 \r
- 如果您希望代码独立于平台并且您将在生成它的同一平台上使用数据,您可以使用
String.format("%n")
、System.getProperty("line.separator")
或 System.lineSeparator()
对于遇到这个问题的其他人,同时可能尝试做我过去一直在做的事情,使用 Eclipse 的源菜单项 Generate toString()... 和模板。我曾经尝试将其作为模板:
${object.className} [\n${member.name()}=${member.value}, ${otherMembers}]
但是,这导致了以下示例:
@Override
public String toString() {
return "NEDCustomer [\nsubscriberType=" + subscriberType + "]";
注意:“\n”被替换为“\n”,这显然在输出控制台或日志中不起作用,所以我会快速搜索并替换以替换双反斜杠与一个。
随着新的 eclipse 工作区的启动和运行,我最终决定再次快速 google 并找出是否有更好的方法来做到这一点,在这种情况下我发现了这个 SO 问题,但是在找到它之前我注意到 tostring templates 的 eclipse.org link 所以我回到那个网页,因为这个问题的答案对我来说不是主题,它就像编辑模板一样简单:
${object.className} [
${member.name()}=${member.value},
${otherMembers}]
eclipse 现在可以正确生成以下内容(我最终确实为 clarity/formatting 添加了一个选项卡):
@Override
public String toString() {
return "NEDCustomer [\n\tsubscriberType=" + subscriberType + "]";
再说一次,这并不能完全回答 OP 的问题,因为他们手动创建了 toString 方法,但是通过使用 Eclipse 的 toString 模板可以节省大量时间,特别是如果您有 20 个以上的变量,它会以某种方式做到或增强它。
希望这可能对其他人有所帮助
我想在 TestBook.java 文件中写 System.out.println(b)
而不是许多 system.out.println()
行。
那么我应该在 toString()
中写什么 class 到 return 相同的结果如下所示
//Title: xxx
//Author:xxx
//Years:xxx
//Pages:xxx
//edition: xxxx
//==================================
public class Book {
String title;
String author;
int yearOfPublishing;
int numberOfPages;
int eddition;
Book ()
{}
Book ( String title, String author, int yop, int nop, int eddition)
{
this.title = title;
this.author = author;
yearOfPublishing = yop;
numberOfPages = nop;
this.eddition = eddition;
}
public String toString()
{
// return what?? how can i return new lines
}
}
public class TestBook {
public static void main(String[] args) {
Book b = new Book("Data", "Joe", 2015, 276, 3);
System.out.println ( "Title : " +b.title);
System.out.println ( "Author : " +b.author);
System.out.println ( "Year : " +b.yearOfPublishing);
System.out.println ( "Pages : " +b.numberOfPages);
System.out.println ( "Eddition : " +b.eddition);
System.out.println ("==================================");
}
}
您可以 return 换行符与您要格式化的数据内联:
return "Title : " + title + "\nAuthor : " + author ...
请注意,这可能是也可能不是解决此问题的最佳方法。
这可能是我给出的最短的答案。
Google这个:
\n
- 如果在OSX之前总是在*nix、Windows或Mac上消耗输出,则可以分别使用
\n
、\r\n
或\r
- 如果您希望代码独立于平台并且您将在生成它的同一平台上使用数据,您可以使用
String.format("%n")
、System.getProperty("line.separator")
或System.lineSeparator()
对于遇到这个问题的其他人,同时可能尝试做我过去一直在做的事情,使用 Eclipse 的源菜单项 Generate toString()... 和模板。我曾经尝试将其作为模板:
${object.className} [\n${member.name()}=${member.value}, ${otherMembers}]
但是,这导致了以下示例:
@Override
public String toString() {
return "NEDCustomer [\nsubscriberType=" + subscriberType + "]";
注意:“\n”被替换为“\n”,这显然在输出控制台或日志中不起作用,所以我会快速搜索并替换以替换双反斜杠与一个。
随着新的 eclipse 工作区的启动和运行,我最终决定再次快速 google 并找出是否有更好的方法来做到这一点,在这种情况下我发现了这个 SO 问题,但是在找到它之前我注意到 tostring templates 的 eclipse.org link 所以我回到那个网页,因为这个问题的答案对我来说不是主题,它就像编辑模板一样简单:
${object.className} [
${member.name()}=${member.value},
${otherMembers}]
eclipse 现在可以正确生成以下内容(我最终确实为 clarity/formatting 添加了一个选项卡):
@Override
public String toString() {
return "NEDCustomer [\n\tsubscriberType=" + subscriberType + "]";
再说一次,这并不能完全回答 OP 的问题,因为他们手动创建了 toString 方法,但是通过使用 Eclipse 的 toString 模板可以节省大量时间,特别是如果您有 20 个以上的变量,它会以某种方式做到或增强它。
希望这可能对其他人有所帮助