Java对象解构 [英] Java object destructuring
问题描述
在javascript中存在对象分解,因此,如果中间对象被重复使用多次,我们可以分解对象并仅使用结束键.例如)
In javascript there is object destructuring so we can break down objects and just use the end key if the intermidiate objects are resused multiple times. e.g)
const person = {
firstName: "Bob",
lastName: "Marley",
city: "Space"
}
因此,我们无需调用 person.<>
来获取每个值,就可以像这样
So instead of calling person.<>
to get each value we can destructure it like this
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对象,我需要从中获取值,并且必须像这样调用长的中间对象名称:
Is there something similar in Java? I have this Java Object that I need to get the value from and have to call long intermediate object names like this:
myOuterObject.getIntermediateObject().getThisSuperImportantGetter()
myOuterObject.getIntermediateObject().getThisSecondImportantGetter()
...
我希望以某种方式对其进行破坏,只需调用最后一个方法 getThisSuperImportantGetter()
, getThisSecondImportantGetter()
即可获得更简洁的代码.
I would like this destructure it somehow and just call the last method getThisSuperImportantGetter()
, getThisSecondImportantGetter()
for cleaner code.
推荐答案
Java Language Architect Brian Goetz 最近谈到要在即将发布的Java版本中添加解构功能.在他的论文中找到 Sidebar:模式匹配一章:
Java Language Architect Brian Goetz has recently talked about adding destructuring to an upcoming version of Java. Look for the Sidebar: pattern matching chapter in his paper:
我非常不喜欢当前的语法建议,但是根据Brian的说法,您的用例将如下所示(请注意,目前这仅是一个建议,将不适用于当前的任何版本)Java ):
I strongly dislike the current proposal of the syntax, but according to Brian your use case will look like the following (please note, that at this point this is a proposal only and will not work with any current version of 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检查中使用该解构模式,例如:
You should than be able to use that deconstruction pattern for instance in an instanceof check like so:
if (o instanceof Person(var firstName, lastName, city)) {
System.out.println(firstName);
System.out.println(lastName);
System.out.println(city);
}
抱歉,Brian在示例中没有提到任何直接的销毁任务,我不确定是否以及如何支持这些任务.
Sorry, Brian does not mention any direct destructuring assignment in his examples, and I'm not sure if and how these will be supported.
旁注:我确实看到了与构造函数的预期相似性,但是我个人不太喜欢当前的提议,因为解构函数"的参数感觉像是超参数(布莱恩在他的论文中说了很多).对我来说,这在每个人都在谈论不变性并使您的方法参数成为 final
的世界中,是一种反直观的行为.
On a side note: I do see the intended similarity to the constructor, but I personally do not like the current proposal that much, because the arguments of the "deconstructor" feel like out-parameters (Brian says as much in his paper). For me this is rather counter-intuitiv in a world where everybody is talking about immutability and making your method parameters final
.
我希望Java越过篱笆而支持多值返回类型.类似于以下内容:
I would rather like to see Java jump over the fence and support multi-value return types instead. Something along the lines of:
public (String firstName, String lastName, String city) deconstruct() {
return (this.firstName, this.lastName, this.city);
}
这篇关于Java对象解构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!