您可以开始连接这些String
值,例如...
String output;
output = "Name: " + this.Name;
output += "Address: " + this.Address;
output += "Balance: " + this.balance;
output += "Minutes used" + this.minutes;
output += "Texts Sent: " + this.texts;
output += "Cost per Minute: " + this.unitMinuteCost;
output += "Cost per Text: " + this.unitTextCost;
或者您可以StringBuilder
用于相同的任务
StringBuilder output = new StringBuilder(128);
output.append("Name: ").append(this.Name);
output.append("Address: ").append(this.Address);
output.append("Balance: ").append(this.balance);
output.append("Minutes used").append(this.minutes);
output.append("Texts Sent: ").append(this.texts);
output.append("Cost per Minute: ").append(this.unitMinuteCost);
output.append("Cost per Text: ").append(this.unitTextCost);
return output.toString();
但编译器会自动为您执行此操作。如果您在循环中创建输出,则建议使用 a StringBuilder
,因为编译器本身无法进行这种优化