Java 对象解构
Java object destructuring
在javascript中有对象解构,所以我们可以分解对象,如果多次重新使用中间对象,只需使用结束键。例如)
const person = {
firstName: "Bob",
lastName: "Marley",
city: "Space"
}
因此,我们可以像这样解构它,而不是调用 person.<>
来获取每个值
console.log(person.firstName)
console.log(person.lastName)
console.log(person.city)
解构:
const { firstName, lastName, city } = person;
然后这样调用:
console.log(firstName)
console.log(lastName)
console.log(city)
Java中有类似的东西吗?我有这个 Java 对象,我需要从中获取值并且必须像这样调用长中间对象名称:
myOuterObject.getIntermediateObject().getThisSuperImportantGetter()
myOuterObject.getIntermediateObject().getThisSecondImportantGetter()
...
我希望以某种方式对其进行解构,然后调用最后一个方法 getThisSuperImportantGetter()
、getThisSecondImportantGetter()
以获得更清晰的代码。
据我所知,java不支持这个。
其他名为 Kotlin 的 JVM 语言支持此功能
Java 语言架构师 Brian Goetz 最近谈到要在 Java 的即将发布的版本中添加解构。在他的论文中查找 侧边栏:模式匹配 章节:
我非常不喜欢当前的语法提议,但根据 Brian 的说法,您的用例将如下所示(请注意,目前这只是一个提议,不适用于Java 的任何当前版本):
public class Person {
private final String firstName, lastName, city;
// Constructor
public Person(String firstName, String lastName, String city) {
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
}
// Deconstruction pattern
public pattern Person(String firstName, String lastName, String city) {
firstName = this.firstName;
lastName = this.lastName;
city = this.city;
}
}
您应该能够在 instanceof 检查中使用该解构模式,例如:
if (o instanceof Person(var firstName, lastName, city)) {
System.out.println(firstName);
System.out.println(lastName);
System.out.println(city);
}
抱歉,Brian 在他的示例中没有提到任何直接的解构赋值,我不确定这些是否以及如何得到支持。
旁注:我确实看到了与构造函数的预期相似性,但我个人不太喜欢当前的提案,因为 "deconstructor" 感觉像是超出参数(Brian 在他的论文中说了很多)。对我来说,在每个人都在谈论不变性并使您的方法参数 final
的世界中,这是相当违反直觉的。
我更愿意看到 Java 跳过栅栏并改为支持多值 return 类型。大致如下:
public (String firstName, String lastName, String city) deconstruct() {
return (this.firstName, this.lastName, this.city);
}
在javascript中有对象解构,所以我们可以分解对象,如果多次重新使用中间对象,只需使用结束键。例如)
const person = {
firstName: "Bob",
lastName: "Marley",
city: "Space"
}
因此,我们可以像这样解构它,而不是调用 person.<>
来获取每个值
console.log(person.firstName)
console.log(person.lastName)
console.log(person.city)
解构:
const { firstName, lastName, city } = person;
然后这样调用:
console.log(firstName)
console.log(lastName)
console.log(city)
Java中有类似的东西吗?我有这个 Java 对象,我需要从中获取值并且必须像这样调用长中间对象名称:
myOuterObject.getIntermediateObject().getThisSuperImportantGetter()
myOuterObject.getIntermediateObject().getThisSecondImportantGetter()
...
我希望以某种方式对其进行解构,然后调用最后一个方法 getThisSuperImportantGetter()
、getThisSecondImportantGetter()
以获得更清晰的代码。
据我所知,java不支持这个。
其他名为 Kotlin 的 JVM 语言支持此功能
Java 语言架构师 Brian Goetz 最近谈到要在 Java 的即将发布的版本中添加解构。在他的论文中查找 侧边栏:模式匹配 章节:
我非常不喜欢当前的语法提议,但根据 Brian 的说法,您的用例将如下所示(请注意,目前这只是一个提议,不适用于Java 的任何当前版本):
public class Person {
private final String firstName, lastName, city;
// Constructor
public Person(String firstName, String lastName, String city) {
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
}
// Deconstruction pattern
public pattern Person(String firstName, String lastName, String city) {
firstName = this.firstName;
lastName = this.lastName;
city = this.city;
}
}
您应该能够在 instanceof 检查中使用该解构模式,例如:
if (o instanceof Person(var firstName, lastName, city)) {
System.out.println(firstName);
System.out.println(lastName);
System.out.println(city);
}
抱歉,Brian 在他的示例中没有提到任何直接的解构赋值,我不确定这些是否以及如何得到支持。
旁注:我确实看到了与构造函数的预期相似性,但我个人不太喜欢当前的提案,因为 "deconstructor" 感觉像是超出参数(Brian 在他的论文中说了很多)。对我来说,在每个人都在谈论不变性并使您的方法参数 final
的世界中,这是相当违反直觉的。
我更愿意看到 Java 跳过栅栏并改为支持多值 return 类型。大致如下:
public (String firstName, String lastName, String city) deconstruct() {
return (this.firstName, this.lastName, this.city);
}