数据类型:
整数型:
Byte 字节 127
Short 32767
Int 2的32次方-1
Long 2的64次方-1
浮点型 :
Float 32位
var f: Float =1.1F;
Double 64位
var d : Double =1.1;
数字类型基类 :Number;
布尔类型:
Boolean ( true/false)
字符型:
Char
var c:Char = 'c';
var c : Char = "中";
字符串
String
var s :String = "hello kotlin";
s = "hello {3+5}";
集合
Array 数组
var nums = intArrayOf(1,2,3,4,5);
List
var list = listOf(1,2,3,4,5);
Set
var set = setOf(1,2,3);
Map
var map = mapOf(1 to 3, 2 to 4);
其他
var r :IntRange = 0 .. 100;
流程控制
if
if(Boolean){}
if(){
}else if{
}else{
}
int a = if(Boolean) 1 else 2
when
var a = when(){
1-> { "1" }
2->{ "2" }
eles{ "3" }
}
while
while(Boolean){}
for
for(v in arr){
}
for((i,v) in arr.withIndex()){
}
函数
fun myFunction(){
}
//参数
private fun greeting(s: String){
print(" hello it")
}
val tellWang : (String,Any) - >Boolean = {
s, _ ->
"Wang" ==s;
}
fun sum (a : Int ,b:Int ) = a+b
var sum : (Int , Int ) - > Int = { a,b -> a+b}
fun demoLambda(param : (String,Any) -> Boolean){
var re = param("hu","");
print(re);
}
demoLambda(tellWang);
demoLambda(tellLi);
//函数 也是一种特殊类型
类&对象
class myClass{
val v =200;
fun myFun(){
print("hello");
}
}
fun demo1(){
val c = MyClass();
c.v;
c.MyFun();
}
class MyClass(v : Int ){
fun myFun(){
print("hello");
}
}
constructor 构造函数
class MyClass(){
var v :Int;
constructor (par : Int){
v = par;
}
fun myFun(){
print("hello");
}
}
init 初始化
class MyClass(){
init{
}
init{
}
}
get&set
class MyClass(val fName,val lName){
var fuName :String
get() = "lName"
set(value){
val names = value.split(" ")
fName = names[0]
lName = names[1]
}
}
companion object
//静态单例
: 继承
open 重载
data
数据
enum
枚举
sealed
//封闭
object
//单例
网友评论