简短的答案,你不能。
@FunctionalInterface不能用于覆盖的方法
Object。
Formattable但是,您可以使用虚拟扩展方法来实现。注意:下面的代码是未测试的:
@FunctionalInterfacepublic interface ToStringInterface extends Formattable{ String asString(); @Override default void formatTo(Formatter formatter, int flags, int width, int precision) { formatter.format("%s", this); // Or maybe even better: formatter.out().append(this.asString()); }}我建议使用此解决方案,因为您正在使用
String.format()该接口。
或者,您可以定义自己的界面。甚至写了这个接口,它调用的包装
.toString()在
.asString()。选择很多。



