Kotlin学习之变量类型推断和字符串模板

时间:2021-06-18 14:47:14

1.变量类型推断

(1)省去变量类型

Kotlin中大部分情况下不需要说明使用对象的类型,编译器可以直接推断出它的类型

 /**
     * 变量类型推断
     */
    private fun concludeType() {
        //省去变量类型

        val str = "abc"
        val b = str is String
        Log.i("zhoujian", "str=" + str);
        Log.i("zhoujian", "str is String=" + b);
        Log.i("zhoujian", "str::class=" + str::class);
        Log.i("zhoujian", "str::class.java=" + str::class.java);

        Log.i("zhoujian", "-----------------------------------------------------------------1");


        val d = Date()
        val b1 = d is Date
        Log.i("zhoujian", "d=" + d);
        Log.i("zhoujian", "d is Date=" + b1);
        Log.i("zhoujian", "d::class=" + d::class);
        Log.i("zhoujian", "d::class.java=" + d::class.java);

        Log.i("zhoujian", "-----------------------------------------------------------------2");


        val array = arrayOf(1, 2, 3)
        val b2 = array is Array
        Log.i("zhoujian", "array=" + array);
        Log.i("zhoujian", "array is Array=" + b2);
        Log.i("zhoujian", "array::class=" + array::class);
        Log.i("zhoujian", "array::class.java=" + array::class.java);
        
        //编译器能够检测到其类型,自动完成类型转换。当然,我们也可以明确指定变量类型
        //val x: Int = 10

    }

结果:

06-12 09:58:31.243 31161-31161/com.zhoujian.kotlindemo I/zhoujian: str=abc
06-12 09:58:31.243 31161-31161/com.zhoujian.kotlindemo I/zhoujian: str is String=true
06-12 09:58:31.246 31161-31161/com.zhoujian.kotlindemo I/zhoujian: str::class=class java.lang.String (Kotlin reflection is not available)
06-12 09:58:31.247 31161-31161/com.zhoujian.kotlindemo I/zhoujian: str::class.java=class java.lang.String
06-12 09:58:31.247 31161-31161/com.zhoujian.kotlindemo I/zhoujian: -----------------------------------------------------------------1
06-12 09:58:31.251 31161-31161/com.zhoujian.kotlindemo I/zhoujian: d=Tue Jun 12 09:58:31 GMT+08:00 2018
06-12 09:58:31.251 31161-31161/com.zhoujian.kotlindemo I/zhoujian: d is Date=true
06-12 09:58:31.251 31161-31161/com.zhoujian.kotlindemo I/zhoujian: d::class=class java.util.Date (Kotlin reflection is not available)
06-12 09:58:31.251 31161-31161/com.zhoujian.kotlindemo I/zhoujian: d::class.java=class java.util.Date
06-12 09:58:31.252 31161-31161/com.zhoujian.kotlindemo I/zhoujian: -----------------------------------------------------------------2
06-12 09:58:31.252 31161-31161/com.zhoujian.kotlindemo I/zhoujian: array=[Ljava.lang.Integer;@3ec2efd
06-12 09:58:31.252 31161-31161/com.zhoujian.kotlindemo I/zhoujian: array is Array=true
06-12 09:58:31.252 31161-31161/com.zhoujian.kotlindemo I/zhoujian: array::class=class [Ljava.lang.Integer; (Kotlin reflection is not available)
06-12 09:58:31.252 31161-31161/com.zhoujian.kotlindemo I/zhoujian: array::class.java=class [Ljava.lang.Integer;

(2)使用is运算符进行类型检测
 //使用is运算符进行类型检测
    private fun getLength(obj: Any): Int? {
        var resault = 0
        if (obj is String) {
            resault = obj.length
        }
        return resault
    }

2.字符串模板

模板表达式以美元符号($)开始

 val str = "abc"
 val s = "$str has ${str.length} characters"
 Log.i("zhoujian", "s=" + s);
06-12 10:06:50.765 31893-31893/? I/zhoujian: s=abc has 3 characters