Kotlin introduces the wonderful concept of Data Classes. These classes will derive the equals()/hashCode()
, toString()
, getters()/setters()
, and a copy()
function based on the properties declared in the constructor:
data class KotlinUser(val name: String, val age: Int)
在Java中,这看起来像:
public class JavaUser {
public JavaUser(String name, Int age) {
...
}
//getters
//setters
//equals()/hashCode()
//toString()
}
我的问题是关于用Kotlin打包这些数据类文件的问题.来自Java的我会将JavaUser
存储在它自己的类文件中,位于:org.package.foo.JavaUser
下
由于数据类的简单性,我们在Kotlin中是否以相同的方式存储数据类文件?(即, for each 数据类org.package.foo.KotlinUser
个和单独的文件).另外,在一个类文件中存储多个数据类是否不受欢迎?
org.package.foo.DataClasses
包含:
data class Foo(val a: String, val b: String)
data class Bar(val a: Int, val b: Int)
I looked around in the idioms/coding style sections of the Kotlin Documentation and could not find anything about this (maybe I skimmed past it though). What is the best practice?
谢谢