通过idea创建kotlin项目:
创建kotlin文件
package com.czhappy.chapter01
var aBoolean:Boolean = true
var anInt:Int = 9
var anotherInt:Int = 0xFF
var maxInt:Int = Int.MAX_VALUE
var minInt:Int = Int.MIN_VALUE
var aLong:Long = 1234
var maxLong:Long = Long.MAX_VALUE
var minLong:Long = Long.MIN_VALUE
var aFloat:Float = 2.0f
var aDouble:Double = 3.0
var aShort:Short = 127
var byte:Byte = 127
var aChar:Char = '0'
var bChar:Char = '中'
var aString:String = "Hello"
var bString:String = String(charArrayOf('H','e','l','l','o'))
var aRange:IntRange = 0..10//[0,10]
var bRanage:IntRange=0 until 10//[0,10)
var aIntArray:IntArray = intArrayOf(1,3,5,7)
var aCharArray:CharArray = charArrayOf('h', 'e', 'l' , 'l', 'o')
var aStringArray:Array<String> = arrayOf("我", "是", "程序员")
fun main(args: Array<String>) {
println("hello world")
println(aString == bString)//true
println(aString === bString)//false
var arg1:Int = 1
var arg2:Int = 2
println("" + arg1 + "+" + arg2 + "=" + (arg1+arg2))
println("$arg1 + $arg2 = ${arg1+arg2}")
println(50 in aRange)
println(aRange.contains(50))
for(i in aRange){
print("$i ")
}
println()
println(aIntArray.size)
for(i in aIntArray){
print("$i ")
}
println()
println(aCharArray.joinToString(""))
println(aIntArray.slice(1..2))
println(aStringArray.joinToString(""))
}
运行结果:
package com.czhappy.chapter01
open class Human(face:String, voice:String){
init {
println("我new了一个${this.javaClass.simpleName},face:$face voice:$voice")
}
}
class Man(face:String, voice:String):Human(face, voice)
class Woman(face:String, voice:String):Human(face, voice)
fun main(args: Array<String>) {
var Jack:Man = Man("阳光", "浑厚")
var Lucy:Woman = Woman("甜美", "动听")
println(Jack is Human)
}
运行结果: