字符串模版
// 字符串模版
fun main(){
// $ 表示一个变量名或者变量值
// $varNama 表示变量值
//${varName.fun()} 表示变量的方法返回值
val name = "小白"
val age = 28
val sex = 'M'
val info = "ABCDEFG"
println("name:$name, age:$age, sex:$sex, info:$info")
// --- 自己不用关心 \n 换行, 不用自己调整
val infoMessage : String = """
AAAA
BBBB
CCCC
DDDD
"""//前置空格
println(infoMessage)
val infoMessage2 : String = """
AAAA
BBBB
CCCC
DDDD
""".trimIndent()//没空格
println(infoMessage2)
val infoMessage3 : String = """
|AAAA
|BBBB
|CCCC
|DDDD
""".trimMargin("|")//没空格 控制|
println(infoMessage3)
//需求 显示$9999.99
val price = """
${'$'}9999.99
""".trimIndent()
println(price)
}
类型推导
fun main(): Unit {
println("类型推导 Int${add(1,1)}")
println(add2(1,1))
println(add3(1,1))
lenMethod(1,2,3,4,5,6)
//lambda表达式
val addMethod : (Int,Int) -> Int = {num1,num2 -> num1 + num2}
val r = addMethod(9,9)
println(r)
}
//返回类型Int
fun add(num1: Int, num2: Int): Int{
return num1 + num2
}
//返回类型 --> 类型推导 Int
fun add2(num1: Int,num2: Int) = num1 + num2
//返回类型 --> 类型推导 String
fun add3(num1: Int,num2: Int) = "${num1}+${num2}"
//可变参数
fun lenMethod(vararg value: Int){
for (i : Int in value){
println(i)
空检测机制
fun main(){
var info: String? = null
//?如果info是null, 就不执行.length
println(info?.length)
// !! 慎用 == Java
println(info!!.length)
// == Java
if(info != null){
println(info.length)
}
}
//允许返回 null
fun testMethod(name: String) : Int?{
if(name.equals("xb")){
return 9999
}
return null
}
区间
//区间
fun main(){
//1 到 9
for (i: Int in 1..9){
println(i)
}
// 不会输出
for (i: Int in 9..1){
println(i)
}
//大到小
for (i: Int in 9 downTo 1){
println(i)
}
//用区间判断
val value = 88
if(value in 1..100){
println("包含在 1 到 100")
}
//步长指定
for (i: Int in 1..20 step 2){
println(i)
}
//排除最后一个 10
for (i: Int in 1 until 10){
println(i)
}
}
比较两个值
fun main(){
val name1 = "张三"
val name2 = "张三"
//kotlin == 等价 Java的equals
println(name1.equals(name2))
println(name1 == name2)
val test1: Int? = 1000
val test2: Int? = 1000
println(test1 === test2)
}
数组
fun main(){
//第一种形式
val num1 = arrayOf(1,2,3,4,5,6)
println(num1[0])
println(num1[5])
for (num in num1) {
println(num)
}
//第二种形式
val num2 = Array(10) { value: Int -> (value + 20000) }
for (num in num2) {
println(num)
}
}
条件
fun main(){
val num1 : Int = 99999999
val num2 : Int = 88888888
val maxValue = if(num1 > num2) num1 else num2
val max : Int = if(num1 > num2){
println("num1 > num2")
num1
} else {
println("num1 < num2")
num2
}
val x = 80
val y = 20
if(x in 1..10 && y in 1..50){
println("x y 轴 符合")
} else {
println("x y 轴 不符合")
}
}
switch --> when
//switch --> when
fun main(){
val num5 = 745
when(num5){
in 1..100 -> println("1..100")
in 101..500 -> println("101..500")
else -> println("其他")
}
val num6 = 3
val result = when(num6){
1 -> {
println("很开心")
// .. 省略 200 行代码
"今天是星期1"
}
2 -> {
println("很开心")
// .. 省略 200 行代码
"今天是星期2"
}
3 -> {
println("很开心")
// .. 省略 200 行代码
"今天是星期3"
} else -> num6
}
when(8) {
1,2,3,4 -> {
println("满足 1,2,3,4")
}
in 5 .. 8 -> {
println("满足 5..8")
}
else -> {
println("不满足")
}
}
}
循环 与 标签
fun main(){
ttt@ for (i in 1..20){
for(j in 1..20){
println("i:$i, j:$j")
if(i == 5){
break// 内部循环 break
break@ttt //外部循环 break
}
}
}
//循环
var items = listOf<String>("张三","李四","王五")
for (item in items) {
println(item)
}
items.forEach {
println(it)
}
for (index in items.indices) {
println("下标:$index,对应的值:${items[index]}")
}
}
//自带标签
class Test {
val I = "AAAA"
fun show(){
println(I)
println(this.I)
println(this@Test.I)
}
}
类 对象
// 默认 public final class Persion 如果需要继承 加 open
open class Person(id: Int) //主构造
{
//次构造
constructor(id: Int, name: String) : this(id)
constructor(id: Int, sex: Char) : this(id)
constructor() : this(id = 88)
}
class Student() : Person(){
//lateinit 懒加载 没有赋值 就不能使用,否则报错
lateinit var name : String
var age: Int = 0
}
接口 interface
fun main(){
}
//接口默认是 open
interface CallBack{
fun callbackMethod() : Boolean
}
//接口默认是 open
interface CallBack2{
fun callbackMethod() : Boolean
}
// 默认是 public final class Persion 不能被继承 abstract 相当于 open
abstract class Person1 : CallBack , CallBack2{
abstract fun getLayoutId() : Int
abstract fun initView()
}
class Student1 : Person1(){
override fun getLayoutId(): Int {
return 88
}
override fun initView() {
}
override fun callbackMethod(): Boolean {
return false
}
}
data --> java Bean
data class TextUser (
var id: Int,
var name: String,
var sex: Char
)
fun main(){
var user = TextUser(99,"lisi",'M')
val(myId, myName,mySex) = user.copy()
println("myId:$myId,myName:$myName,mySex:$mySex")
//拒收
val (_,myName2,_) = user.copy()
println("myName2:$myName2")
}
object
//object 只实例一次
object MyEngine{
fun m(){
}
}
单例
class TestManger
{
object Holder{
val instance = TestManger()
}
companion object {
fun getInstance() : TestManger = Holder.instance
}
fun show(name: String){
println("show:$name")
}
}
fun main() {
TestManger.getInstance().show("xiaobai")
var test2 : TextManager2? = TextManager2.getInstance()
test2?.show("name")
}
class TextManager2{
private constructor()
companion object {
private var instance : TextManager2? = null
fun getInstance() : TextManager2? {
if(instance == null){
instance = TextManager2()
}
return instance
}
}
fun show(name: String){
println("show:${name}")
}
}
嵌套类 嵌套函数
class Test2 {
val I = "AAA"
//嵌套类
class Sub{
fun show(){
println()
}
class A{
class B{
class C{
}
}
}
}
//这个是 内部类 关键子 inner
inner class Sub2{
fun show(){
println("I:$I")
}
}
//嵌套函数
fun a(){
fun b(){
fun c(){
fun d(){
}
}
}
}
}
网友评论