在Kotlin中var和val的区别是什么?
我已经通过了这个链接:
属性和字段
如本连结所述:
只读属性声明的完整语法不同于
可变1有两种方式:它以val开头,而不是var
不允许设置。
但在此之前有一个使用setter的例子。
fun copyAddress(address: Address): Address {
val result = Address() // there's no 'new' keyword in Kotlin
result.name = address.name // accessors are called
result.street = address.street
// ...
return result
}
var和val的确切区别是什么?
为什么我们两者都需要?
这不是Kotlin中的变量的副本,与Java的区别:'var'和。“val”?因为我问的是与文档中的特定例子有关的疑问,而不仅仅是一般的疑问。
通过将Kotlin反编译为Java,我得到了确切的答案。
如果你在Kotlin中这样做:
data class UsingVarAndNoInit(var name: String)
data class UsingValAndNoInit(val name: String)
你会得到UsingVarAndNoInit:
package classesiiiandiiiobjects.dataiiiclasses.p04variiiandiiival;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
public final class UsingVarAndNoInit {
@NotNull private String name;
@NotNull
public final String getName() {
return this.name;
}
public final void setName(@NotNull String string) {
Intrinsics.checkParameterIsNotNull((Object) string, (String) "<set-?>");
this.name = string;
}
public UsingVarAndNoInit(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
this.name = name;
}
@NotNull
public final String component1() {
return this.name;
}
@NotNull
public final UsingVarAndNoInit copy(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
return new UsingVarAndNoInit(name);
}
@NotNull
public static /* bridge */ /* synthetic */ UsingVarAndNoInit copy$default(
UsingVarAndNoInit usingVarAndNoInit, String string, int n, Object object) {
if ((n & 1) != 0) {
string = usingVarAndNoInit.name;
}
return usingVarAndNoInit.copy(string);
}
public String toString() {
return "UsingVarAndNoInit(name=" + this.name + ")";
}
public int hashCode() {
String string = this.name;
return string != null ? string.hashCode() : 0;
}
public boolean equals(Object object) {
block3:
{
block2:
{
if (this == object) break block2;
if (!(object instanceof UsingVarAndNoInit)) break block3;
UsingVarAndNoInit usingVarAndNoInit = (UsingVarAndNoInit) object;
if (!Intrinsics.areEqual((Object) this.name, (Object) usingVarAndNoInit.name)) break block3;
}
return true;
}
return false;
}
}
你也会得到UsingValAndNoInit:
package classesiiiandiiiobjects.dataiiiclasses.p04variiiandiiival;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
public final class UsingValAndNoInit {
@NotNull private final String name;
@NotNull
public final String getName() {
return this.name;
}
public UsingValAndNoInit(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
this.name = name;
}
@NotNull
public final String component1() {
return this.name;
}
@NotNull
public final UsingValAndNoInit copy(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
return new UsingValAndNoInit(name);
}
@NotNull
public static /* bridge */ /* synthetic */ UsingValAndNoInit copy$default(
UsingValAndNoInit usingValAndNoInit, String string, int n, Object object) {
if ((n & 1) != 0) {
string = usingValAndNoInit.name;
}
return usingValAndNoInit.copy(string);
}
public String toString() {
return "UsingValAndNoInit(name=" + this.name + ")";
}
public int hashCode() {
String string = this.name;
return string != null ? string.hashCode() : 0;
}
public boolean equals(Object object) {
block3:
{
block2:
{
if (this == object) break block2;
if (!(object instanceof UsingValAndNoInit)) break block3;
UsingValAndNoInit usingValAndNoInit = (UsingValAndNoInit) object;
if (!Intrinsics.areEqual((Object) this.name, (Object) usingValAndNoInit.name)) break block3;
}
return true;
}
return false;
}
}
这里有更多的例子:https://github.com/tomasbjerre/yet-another-kotlin-vs-java-comparison