Android第一行代码(第三版)-Kotlin语法

参考:第一行代码

在Project视图下,我的项目名称是Myapp
找到MyApp->app->src->main->java->com.example.myapp路径下,右键点击com.example.myapp,创建一个kotlin文件,名字为LearnKotlin
在这里插入图片描述
learnKotlin.kt文件

package com.example.myapp

import java.lang.NumberFormatException
import kotlin.math.max
import kotlin.math.min

fun main() {
    println("hello world")


    println("测试基础类型"  + " \n" )
    var a: Int = 10
    val i: Int = 10
    val l: Long = 1000
    val s: Short = 100
    val b: Byte = 1
    val f: Float = 10.1F
    val d: Double = 10.02
    val c: Char = 'a'
    val bo: Boolean = true

    println("b= " + b)
    println("s = " + s)
    println("i = " + i)
    println("l = " + l)
    println("f = " + f)
    println("d = " + d)
    println("c = " + c)
    println("bo = " + bo)

    println("测试基本类型运算和var val"  + " \n" )
    println("a = " + (a * 20))
    val left: Int= 10
    val right: Int = 20
    println("sum = " + Sum(left,right))
    println("max = " + Max(left,right))
    println("min = " + Min(left,right))
    println("max2 = " + Max2(left,right))
    println("max3 = " + Max3(left,right))

    println("测试when()使用"  + " \n" )
    println("Tom's score is  = " + getScore("Tom"))
    println("Tom's score is  = " + getScore2("TomCat"))
    println("Tom's score is  = " + getScore2("TomJack"))

    println("测试Number"  + " \n" )
    checkNumber(d)
    checkNumber2(d)


    println("测试for循环0..10表示闭区间[0,10] " + " \n" )
    TestForClose()
    println("测试for循环0 unitl 10表示前开后闭[0,10) "  + " \n" )
    TestForOpen()
    println("测试for循环0 until 10 step 2表示设置步长位2 "  + " \n" )
    TestForOpenAndStep()
    println("测试for循环 downTo 表示从某个值减到某个值"  + " \n" )
    TestDownTo()

    println("测试Person类"  + " \n" )
    val p = Person("jack",20)
    p.name = "jack"
    p.age = 20
    p.eat()

    println("测试继承和构造函数"  + " \n" )
    val student = Student("1870649",90,"Tom",20)

    val student1 = Student2()
    student1.printSome()
    val student2 = Student2("jack",10)
    student2.printSome()
    val student3 = Student2("1870649",98,"jack",20)
    student3.printSome()

    println("测试接口"  + " \n" )
    var studentX = Student3("JACK",20)
    studentX.answerQuestion()
    studentX.readBook()

    println("测试单例模式" )
    println("测试单例模式---使用JAVA创建"  + " \n" )
    val singleTon = SingleTon.getInstance()
    singleTon.testSingle()

    println("测试单例模式---使用KOTLIN创建"  + " \n" )
    KotlinSingleton.singleTest()

    println("测试集合list---使用KOTLIN创建"  + " \n" )
    val listFruit = listOf("Apple","balana","orange")            //使用mutableListOf可以对集合进行增改删除操作
    for (fruit in listFruit)
    {
        println(fruit)
    }

    val newList = listFruit.filter { it.length <= 5 }.map { it.toUpperCase() }//使用filter筛选出长度小于5的水果并转化成大写
    for (fruit in newList)
    {
        println(fruit)
    }

    println("测试集合set,没有重复元素---使用KOTLIN创建"  + " \n" )
    val setFruit = mutableSetOf("apple","pear","orabge")
    setFruit.add("strewberry")
    for (fruit in setFruit)
    {
        println(fruit)
    }



    println("测试集合map---使用KOTLIN创建"  + " \n" )
    val mapFruit = mutableMapOf("Apple" to 1,"orange" to 2,"strawberry" to 3)
    mapFruit["pear"] = 4
    mapFruit.put("Apple",2) //键值不能重复
    for ((fruit,number) in mapFruit)
    {
        println("fruit is $fruit , number is  $number")  //KOTLIN写法  等同于:println("fruit is " + fruit + " number is " +  number)
    }

    println("判空辅助?.  "+ " \n" )
    val isNullStringTest: String = ""
    println(getLength(isNullStringTest))

    println("使用函数参数默认值  "+ " \n" )
    defaultVale("hello")
    
}


fun defaultVale(str: String,i: Int = 10)
{
    println("string is + $str , number is $i .")
}
fun getLength(str : String) = str?.length?:0


fun Sum(a:Int,b:Int): Int{
    return a+b
}

fun Max(a:Int,b:Int):Int
{
    return max(a,b)
}

fun Max2(a: Int,b: Int):Int
{
    var value = 0
    if(a>b)
        value = a
    else
        value = b
    return value
}
fun Max3(a: Int,b: Int):Int = if (a>b) a else b

fun Min(a: Int,b:Int):Int = min(a,b)

fun getScore(name: String) = when(name){
    "Tom" -> 86
    "Jack" -> 99
    else -> 0
}

fun getScore2(name: String) = when
{
    name.startsWith("Tom") -> 98
    name == "Tom" -> 98
    name == "Jack" -> 95
    else -> 0

}

fun checkNumber(number: Number)
{
    when(number)
    {
        is Int -> println("number is int")
        is Double -> println("number is double")
        else -> println("number is not support")
    }
}

fun checkNumber2(number: Number) = when
{
    number is Int -> println("number is int")
    number is Double -> println("number is double")
    else -> println("number is not support")
}

fun TestForClose()
{
    for (i in 0..10)
        println(i)
}

fun TestForOpen(){
    for(i in 0 until 10)
        println(i)
}

fun TestForOpenAndStep(){
    for(i in 0 until 10 step 2)
        println(i)
}

fun TestDownTo()
{
    for (i in 10 downTo 1)
    {
        println(i)
    }
}


创建单例模式文件KotlinSingleton.kt

package com.example.myapp

object KotlinSingleton {
    fun singleTest()
    {
        println("this is a singleTon designing by Kotlin language")
    }
}

创建Person.kt文件

package com.example.myapp

import android.support.v4.app.INotificationSideChannel

open class Person (var name:String,var age:Int){
    fun eat()
    {
        println(name + "is eating.\n" + "His age id "+ age + ".")
    }

}

class Student(val number:String,val grade:Int,name: String,age: Int) : Person(name,age) //主构造函数
{
    init{
        println("number is " + number)
        println("grade is " + grade)
        println("name is "+ name)
        println("age is " + age)
        println("\n")
    }
}

class Student2(val number:String,val grade:Int,name: String,age: Int) : Person(name,age){
    constructor(name:String,age:Int):this("",0,name,age){}      //次构造函数

    constructor():this("",0){}                                     //次构造函数

    fun printSome()
    {
        println("number is " + number)
        println("grade is " + grade)
        println("name is "+ name)
        println("age is " + age)
        println("\n")
    }
}

interface Study{
    fun readBook()
    fun answerQuestion()

    fun doHomeWork()
    {
        println("this is a default implemention. ")
    }
}

class Student3(name: String,age: Int):Person(name,age),Study{
    override fun readBook() {
        println(name + " is reading book.")
    }

    override fun answerQuestion() {
        println(name + " is answering question.")
    }
}

创建JAVA格式的单例模式

package com.example.myapp;

public class SingleTon {
    private static SingleTon instance = null;

    private  SingleTon()
    {

    }

    public static SingleTon getInstance()
    {
        if (instance == null)
        {
            instance = new SingleTon();
        }
        return instance;
    }

    public void testSingle()
    {
        System.out.println("调用单例模式");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值