Kotlin
空指针检查
fun checkNull(context:Context?){
if (context != null) {
context.getString(R.string.app_name)
}
context?.getString(R.string.app_name)
//危险的做法 context 一定不为null
context!!.getString(R.string.app_name)
var con = context.let {
it.getString(R.string.app_name)
}
var java = con::class.java
Log.e("vic-zgt","con = ${con} javaClass ${java}");
}
常用标准函数
with
fun testWith(){
// with(obj) 函数 有返回值 obj 是上下文
val str = with(StringBuilder()){
append("1")
append("2")
toString()
}
// 输入 12
Log.e("vic-zgt","${str}")
}
run
fun testRun(){
// obj.run obj 是上下文 有返回值
val str = StringBuilder().run {
append(1)
append(2)
toString()
}
// 输入 12
Log.e("vic-zgt","${str}")
}
apply
fun testApply(){
//obj.apply obj 是上下文 返回对象本身
val builder = StringBuilder().apply {
append(1)
append(2)
}
builder.append(3)
// 输入 123
Log.e("vic-zgt","${builder}")
}
静态方法
fun testUti(){
Utils.getAppName();
Util.getAppName2();
}
//单例类
//public static final Utils INSTANCE = new Utils();
object Utils{
fun getAppName(){
Log.e("vic-zgt","DemoApp");
}
}
//静态方法
class Util{
fun getAppName(){
Log.e("vic-zgt","DemoApp");
}
//会生成一个
//public static final Companion Companion = new Companion((DefaultConstructorMarker) null);
companion object{
fun getAppName2(){
Log.e("vic-zgt","DemoApp2");
}
/*
标记上@JvmStatic 会生成一个静态方法
@JvmStatic
public static final void getAppName3() {
Companion.getAppName3();
}
*/
@JvmStatic
fun getAppName3(){
Log.e("vic-zgt","DemoApp3");
}
}
}
内联函数
//内联函数 inline
inline fun aANDb(a:Int,b:Int,and:(a:Int,b:Int)->Int):Int{
return and(a,b);
}
fun test(){
//如果是内联函数
//编译后就会变成
//int a$iv = 1 + 2;
var r = aANDb(1,2){a,b->
a+b
}
}
noinline
inline fun inlineTest(block1:()->Unit,noinline block2: () -> Unit){
block1()
block2()
}
fun test(){
/*
编译后 就编程如下
Log.e("vic-zgt", "block1");
Demo01Activity$test$2.INSTANCE.invoke();
*/
inlineTest({
Log.e("vic-zgt","block1");
},{
Log.e("vic-zgt","block2");
})
}
中缀函数
/*
中缀函数翻译成Java
public final boolean beginWith(String $this$beginWith, String prefix) {
Intrinsics.checkParameterIsNotNull($this$beginWith, "$this$beginWith");
Intrinsics.checkParameterIsNotNull(prefix, "prefix");
return StringsKt.startsWith$default($this$beginWith, prefix, false, 2, (Object) null);
}
public final void test() {
boolean beginWith = beginWith("abc", "a");
}
*/
//中缀函数
infix fun String.beginWith(prefix:String):Boolean{
return startsWith(prefix)
}
fun test(){
var b = "abc" beginWith "a"
}
高阶函数
class Title{
fun txt(str:String){
println(str)
}
}
class Header{
fun title(init:Title.()->Unit):Title{
var t = Title()
t.init()
return t;
}
}
class HTML{
fun header(init:Header.()->Unit):Header{
var h = Header()
h.init()
return h;
}
fun body(){
println("HTML BODY")
}
}
fun html(init:HTML.()->Unit):HTML{
var html = HTML()
html.init()
return html
}
fun main() {
var html = html {
header{
title{
txt("标题内容")
}
}
body()
}
println(html)
}
扩展系统函数
fun SharedPreferences.open(block:SharedPreferences.Editor.()->Unit){
val editor = edit()
editor.block()
editor.apply()
}
fun test(context:Context){
context.getSharedPreferences("test",Context.MODE_PRIVATE).open {
putBoolean("b",true)
putString("str","zgt")
}
}
lazy 函数的本质
class Later<T> (var block:()->T){
var value:Any? =null;
operator fun getValue(any:Any?,prop:KProperty<*>):T{
if (value == null){
value = block()
}
return value as T;
}
}
fun <T>later( block: () -> T) = Later(block)
class Demo1{
val name:String by later {
println("name")
"abcde"
}
}