Data classes 官方介绍
在学习之前带着问题往往能够收获颇丰
1 输出结果是什么?
data class Student(var name: String = ""){
var age:Int = 0
}
val a = Student(name = "1")
val b = Student(name = "1")
Log.d("GerryLiang", "a equals b ?${a.equals(b)}")
Log.d("GerryLiang", "a == b ?${a == b}")
Log.d("GerryLiang", "a === b ?${a === b}")
2 输出结果是什么?
data class Student(var name: String = ""){
var age:Int = 0
}
val a = Student(name = "1").apply {
age = 10
}
val b = Student(name = "1").apply {
age = 20
}
Log.d("GerryLiang", "a equals b ?${a.equals(b)}")
Log.d("GerryLiang", "a == b ?${a == b}")
Log.d("GerryLiang", "a === b ?${a === b}")
3 输出的结果是什么?
data class Student(var name: String) {
override fun toString(): String {
return "Gerry toString"
}
}
val a = Student(name = "1")
Log.d("GerryLiang", "$a")
4 下面哪些data class 可以编译通过?
data class Student()
abstract data class Student2(var name: String = "")
open data class Student3(var name: String = "", var age: Int = 0)
inner data class Student4(var name: String = "", var age: Int = 0)
sealed data class Student5(var name: String = "", var age: Int = 0)
下面开始公布答案:
- true true false (没有笔误,可以自测下)
- true true false (没有笔误,可以自测下)
- Gerry toString
- 都不可以
如何学习? 查看反编译后的Java代码即可!
如下所示一个简单的 data class文章来源:https://www.toymoban.com/news/detail-807337.html
data class DataBean(var title: String = "", val subTitle: String = "")
我们来看看反编译后的Java代码长什么样子文章来源地址https://www.toymoban.com/news/detail-807337.html
public final class DataBean {
// 每个属性 内部生成了一系列的 get 和 set 方法
@NotNull
private String title;
@NotNull
private final String subTitle;
@NotNull
public final String getTitle() {
return this.title;
}
public final void setTitle(@NotNull String var1) {
Intrinsics.checkNotNullParameter(var1, "<set-?>");
this.title = var1;
}
@NotNull
public final String getSubTitle() {
return this.subTitle;
}
public DataBean(@NotNull String title, @NotNull String subTitle) {
Intrinsics.checkNotNullParameter(title, "title");
Intrinsics.checkNotNullParameter(subTitle, "subTitle");
super();
this.title = title;
this.subTitle = subTitle;
}
// $FF: synthetic method
public DataBean(String var1, String var2, int var3, DefaultConstructorMarker var4) {
if ((var3 & 1) != 0) {
var1 = "";
}
if ((var3 & 2) != 0) {
var2 = "";
}
this(var1, var2);
}
public DataBean() {
this((String)null, (String)null, 3, (DefaultConstructorMarker)null);
}
@NotNull
public final String component1() {
return this.title;
}
@NotNull
public final String component2() {
return this.subTitle;
}
@NotNull
public final DataBean copy(@NotNull String title, @NotNull String subTitle) {
Intrinsics.checkNotNullParameter(title, "title");
Intrinsics.checkNotNullParameter(subTitle, "subTitle");
return new DataBean(title, subTitle);
}
// $FF: synthetic method
public static DataBean copy$default(DataBean var0, String var1, String var2, int var3, Object var4) {
if ((var3 & 1) != 0) {
var1 = var0.title;
}
if ((var3 & 2) != 0) {
var2 = var0.subTitle;
}
return var0.copy(var1, var2);
}
// 以后不用自己拼接字符串打印参数了
@NotNull
public String toString() {
return "DataBean(title=" + this.title + ", subTitle=" + this.subTitle + ")";
}
// 重写了hashCode
public int hashCode() {
String var10000 = this.title;
int var1 = (var10000 != null ? var10000.hashCode() : 0) * 31;
String var10001 = this.subTitle;
return var1 + (var10001 != null ? var10001.hashCode() : 0);
}
// 重写了equals方法
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof DataBean) {
DataBean var2 = (DataBean)var1;
if (Intrinsics.areEqual(this.title, var2.title) && Intrinsics.areEqual(this.subTitle, var2.subTitle)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
到了这里,关于Kotlin学习第1篇——data class的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!