以下代码按您的需要执行。
public class Logger {
public static void main(String[] args) {
new Logger().info("Info: param1 is ?, param2 is ?", new String[] { "a", "b" });
new Logger().info("Info: param1 is ?, param2 is ?", "a", "b");
}
public void info(final String message, final String... args) {
System.out.printf(message.replace("?", "%s") + "%n", args);
}
}
但是,您应该考虑使用正确格式的占位符而不是“?”。例如:
public class Logger {
public static void main(String[] args) {
new Logger().info("Info: param1 is %s, param2 is %s", new String[] { "a", "b" });
new Logger().info("Info: param1 is %s, param2 is %s", "a", "b");
}
public void info(final String message, final String... args) {
System.out.printf(message + "%n", args);
}
}
两个版本都将打印:
Info: param1 is a, param2 is b
Info: param1 is a, param2 is b