package com.example.learningkotlin.day1
import android.util.Log
/**
* @author : Maple
* @date :2021/1/6 4:50 PM
* @description :
*/
//顶层属性
private const val KEY_KOTLIN = "KOTLIN"
fun joinToString() {
println("调用顶层函数")
}
/*
这里相当于我为String类添加一个扩展函数。
然后通过String.isHeHe() 返回一个boolean值
*/
fun String.isHeHe(): Boolean = this == "呵呵"
fun log(s: String) = Log.d("Tag", s)
//不可变扩展属性必须有get函数
val String.number: String
get() {
if (this == "1") {
return "16666666666"
} else if (this == "2") {
return "15555555555"
} else {
return ""
}
}
//可变扩展属性必须有get set函数
var String.address: String
get() {
if (this == "1") {
return "北京"
} else if (this == "2") {
return "上海"
} else {
return ""
}
}
set(value) {
}
/*
这里是编译后的java文件
public final class TopLevelKt {
private static final String KEY_KOTLIN = "KOTLIN";
public static final void joinToString() {
String var0 = "调用顶层函数";
boolean var1 = false;
System.out.println(var0);
}
public static final boolean isHeHe(@NotNull String $this$isHeHe) {
Intrinsics.checkNotNullParameter($this$isHeHe, "$this$isHeHe");
return Intrinsics.areEqual($this$isHeHe, "呵呵");
}
public static final int log(@NotNull String s) {
Intrinsics.checkNotNullParameter(s, "s");
return Log.d("Tag", s);
}
@NotNull
public static final String getNumber(@NotNull String $this$number) {
Intrinsics.checkNotNullParameter($this$number, "$this$number");
if (Intrinsics.areEqual($this$number, "1")) {
return "16666666666";
} else {
return Intrinsics.areEqual($this$number, "2") ? "15555555555" : "";
}
}
@NotNull
public static final String getAddress(@NotNull String $this$address) {
Intrinsics.checkNotNullParameter($this$address, "$this$address");
if (Intrinsics.areEqual($this$address, "1")) {
return "北京";
} else {
return Intrinsics.areEqual($this$address, "2") ? "上海" : "";
}
}
public static final void setAddress(@NotNull String $this$address, @NotNull String value) {
Intrinsics.checkNotNullParameter($this$address, "$this$address");
Intrinsics.checkNotNullParameter(value, "value");
}
}
*/
@Test
fun test1() {
//调用顶层函数
joinToString()
println("hehe".isHeHe())
println("呵呵".isHeHe())
println("1".address)
println("2".number)
}
调用顶层函数
false
true
北京
15555555555