我正在尝试验证我的理解字符串插值和字符串模板。
说两个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支持插值。