问题标签 [data-class]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
53772 浏览

kotlin - Kotlin 数据类复制方法不深度复制所有成员

有人能解释一下copyKotlin 数据类的方法是如何工作的吗?对于某些成员来说,似乎实际上并未创建(深层)副本,并且引用仍然是原始的。

输出:
foo : Foo(a=5, bar=Bar(x=0), list=[1, 2, 3])
foo : Foo(a=10, bar=Bar(x=2), list=[1 , 2, 3, 4])
fooCopy: Foo(a=5, bar=Bar(x=2), list=[1, 2, 3, 4])
barCopy: Bar(x=0)

为什么是barCopy.x=0(预期的),但是fooCopy.bar.x=2(我认为它会是 0)。由于Bar也是一个数据类,我希望在执行foo.bar时也是一个副本foo.copy()

要深度复制所有成员,我可以这样做:

fooCopy: Foo(a=5, bar=Bar(x=0), list=[1, 2, 3])

但是我是否遗漏了什么,或者有没有更好的方法来做到这一点而无需指定这些成员需要强制进行深层复制?

0 投票
2 回答
1154 浏览

kotlin - Reference outside the sealed class in Kotlin?

I'm trying to create a class that uses its own state to operate on the state of an external object that it holds a reference to. The external object can be of class A or B, which are similar, but not controlled by the author. So a sealed class is created to access their common attributes, per this earlier answer from @SimY4.

The problem now is that I've figured out I can't operate on the external object in the way I want, because a sealed class can't refer to its outer class members. Is there a better way to make this work, even if using a different approach (other than sealed class), while:

  • not changing foreign classes A or B;
  • respecting that A and B (and many others in the real case) are similar, so I'm trying to write one tool that calculates and adds magic to A and B with the same code base; and
  • noting that although the ABTool tools are the same, the way they are applied to add magic is slightly different in A vs. B, just as the to access the conceptually common elements of A and B may be different.

Any thoughts on this or a similar workaround? Maybe a more functional approach that I haven't conceived yet?

0 投票
1 回答
727 浏览

kotlin - 处理多个对象实例的最佳方式

部分原因是我无法在 Kotlin 中创建没有参数的数据类,我object在这些情况下使用 s,例如

问题是,有时,我最终会得到Leaf该类的多个实例。显然这通常不应该发生,但是在使用某些框架和某些测试用例进行序列化和反序列化时会发生。

现在,您可能会争辩说我应该修复这些情况,但很难知道它们可能在哪里,而且修改框架的反序列化语义并不总是可能或可取的。

因此,我希望我的 s 的所有实例都object充当相同的值,就像无参数data class(或case classScala 中的无参数)一样。

到目前为止,我最好的解决方案如下,包含在object我创建的每个可能遇到此问题的文件中:

显然,这是冗长且容易出错的,因为似乎不可能将这些实现抽象为接口。超类可以在对象不需要扩展另一个类的情况下工作,但通常情况并非如此。

或者,我可以创建一个带有Nothing参数的数据类。但这似乎更像是一种黑客行为。

其他人是如何处理这个问题的?是否有计划向遵循这些类的假定语义的对象添加hashCode和实现(所有实例都应该相等/相同的 hashCode)?equals

0 投票
1 回答
1943 浏览

kotlin - 搜索 kotlin 空数据类主构造函数的解决方法

使用给定的 kotlin 代码:

没有问题BarEvent

但是由于FooEvent没有比 中的参数更多的参数Event,我希望它有空的构造函数。它没有被授权用于数据类,所以我把它变成了一个对象。但是对象是单例的,因此它不会表现为实例化事件。

我看到的唯一解决方法(将类保持为 a data class)是这样的:

但它不是很优雅。

0 投票
1 回答
6364 浏览

android - 将 POJO 转换为数据类 Kotlin

最近我开始将我的 Android 项目从 Java 转换为 Kotlin。我在许多类中使用 Retrofit 从 API 获取数据。在我的项目中,有很多 POJO,我需要手动将它们转换为数据类,因为它不适用于 Android Studio 的自动转换器工具。

考虑我有模型类:

有什么好方法可以直接将这个模型类转换为 Kotlin 中的数据类吗?这个类手动转换是可以的,但是我有一些类有很多变量,手动转换成数据类会很麻烦。

0 投票
3 回答
1643 浏览

arraylist - 带有字段 ArrayList 的数据类复制 - 更改复制类的 ArrayList 更改原始

我有一个这样的数据类

我有两个对象副本:

然后我更改对象 person 的 personConsents 元素 - 我添加/删除/编辑它们。但由于某种原因,我看到 originalPerson 对象中发生了我不想成为的相同变化。originalPerson 根本不应该改变。怀疑 ArrayList 引用有问题,但需要你的建议我能做什么?最后,我需要比较像fun dataChanged(): Boolean = originalPerson != personbu 这样的两个对象,当 ArrayList 发生变化时它不起作用。

0 投票
1 回答
1251 浏览

kotlin - 具有不同支持字段类型的 Kotlin 数据类

我有一个用于 JSON 序列化的简单类。为此,外部接口使用Strings,但内部表示不同。

我在 Kotlin 中有这个工作(不确定是否有更好的方法)。但是非 val/var 构造函数阻止我使用data.

我如何让这个工作作为一个data class

0 投票
1 回答
984 浏览

android - Kotlin 中的数据类

我需要使用数据类显示图像

我在数据类中感到困惑,我将图像处理为图像或 Int 数据类 User(val pic : Image)

或数据类 User(val pic : Int)

并显示它抛出 CustomAdapter

我知道我的代码不完整,所以我只需要示例来演示数据类中的图像

0 投票
2 回答
9992 浏览

java - 数据类 Kotlin 转 Java 类

我有一个 kotlin 数据类,我试图从 Java 方法中调用它。

从 Java ,我正在尝试保存 Item 列表,因此我需要实例化 Data 类。我不明白我该怎么做。

0 投票
2 回答
5468 浏览

inheritance - Kotlin 中数据类的继承

我知道 Kotlin 从数据类继承存在限制。在讨论这个问题时,我没有学到更多东西。

由于 Kotlin 中的数据类类似于 Java 中的 POJO。我们不应该在 Java POJO 类中也遵循继承吗?总结一下,是因为 Kotlin 的限制不允许我们从数据类继承还是设计有缺陷。

把它分解成一个更简单的问题。从 Java 中的 POJO 类继承是错误的吗?