2

我正在尝试验证我对字符串插值和字符串模板的理解。

说这两个 Java 代码片段是模板的例子是否正确?

public class Person {
 //showing only relevant code
 public String toString() {
   return "Name: " + name + " salary: " + salary + " address: " + address;
 }
}

public String toString() {
  return String.format("name: %s salary: %d address: %s", name, salary, address);
}

这个 Groovy 片段是字符串插值的示例:

public class Person {
  def name
  def salary
  def address

  //showing only relevant parts of the code
  public String toString() {
    return """ name: ${name} salary: ${salary} address: ${address}"""
  }
}

说 Java 和 Python 支持模板但不支持插值是否正确,但 Groovy 确实支持插值。

4

0 回答 0