我正在自学Java,我已经开始使用接口了。我正在使用Cay Horstmann编写的一本名为Big早期对象的教科书。我遇到了一个很难理解的练习。演习内容如下:
System.out.printf方法有用于打印整数、浮点数和其他数据类型的预定义格式.但它也是可扩展的。如果使用S格式,则可以打印实现Formattable接口的任何类。接口只有一个方法:
void formatTo(Formatter formatter, int flags, int width, int precision)在本练习中,您应该让BankAccount类实现Formattable接口。忽略标志和精度,只需使用给定的宽度设置银行余额的格式。为了完成这一任务,您需要获得如下所示的可附加引用:
Appendable a = formatter.out();Appendable是方法的另一个接口。
void append(CharSequence sequence)CharSequence是另一个由String类实现的接口。首先将银行余额转换为字符串,然后用空格填充字符串,使其具有所需的宽度,从而构造字符串。将该字符串传递给追加方法。
我已经到了这样的地步,在一个测试类中,我用$1500美元创建了一个BankAccount对象的实例。给System.out.printf("%s", account)打个电话给我1500个电话。然而,我应该得到的宽度参数,以添加填充,我不知道如何使该工作。
此外,我所拥有的代码对我来说毫无意义。我按照练习的描述编写了代码,但我不知道它是如何工作的。
public class BankAccount implements Formattable
{
private String name;
private double balance;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
this.name = "";
this.balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(String name, double initialBalance)
{
this.name = name;
this.balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public String getName()
{
return this.name;
}
public void formatTo(Formatter formatter, int flags, int width, int precision)
{
Appendable a = formatter.out();
String padding = "";
for(int i = 0; i < width; i++)
{
padding = padding.concat(" ");
}
String bankBalance = padding + String.valueOf(getBalance());
try
{
a.append(bankBalance);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}上面是我的BankAccount类,它实现了Formattable,并定义了formatTo方法。我不明白Appendable a = formatter.out是干什么的。对a.append(bankBalance)的调用如何允许我在测试类中使用System.out.printf(%s, account);。
下面是我的测试课。
public class FormatTester
{
public static void main(String[] args)
{
BankAccount account = new BankAccount("Bob Robert", 1500);
System.out.printf("%s", account);
}
}这个打印1500没有任何填充。我知道没有填充,因为宽度参数没有给出int值。不过,我不知道该如何设置,因为如果我尝试这样做:
public class FormatTester
{
public static void main(String[] args)
{
BankAccount account = new BankAccount("Robert Bob", 1500);
Formatter formatter = new Formatter();
System.out.printf("%s", account.formatTo(formatter, 0, 5, 0);
}
}它抱怨说
方法printf(字符串,对象.)在类型中,PrintStream不适用于参数(字符串、空)
如果我不能这样设置宽度,我怎么能做到呢?顺便说一句,名字并不重要。我试着把名字格式化,但决定暂时保持平衡。
发布于 2019-01-17 13:42:37
解释一下..。
Formatter fmt = new Formatter();
StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
"Fruit Titanesque, Inc.");
fmt.format("%s", sn); // -> "Huge Fruit, Inc."
fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."
fmt.format("%#s", sn); // -> "HUGE"
fmt.format("%-10.8s", sn); // -> "HUGE "
fmt.format("%.12s", sn); // -> "Huge Fruit,*"
fmt.format(Locale.FRANCE, "%25s", sn); // -> " Fruit Titanesque, Inc."同时也出现在文档上。
https://stackoverflow.com/questions/52528094
复制相似问题