欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Koltin09.继承

程序员文章站 2022-09-17 08:22:55
MainActivity.ktpackage com.example.a02handlerimport android.os.Bundleimport android.os.Handlerimport android.os.Messageimport android.widget.ImageViewimport androidx.appcompat.app.AppCompatActivityimport java.lang.ref.WeakReferenceimport java.ma...

MainActivity.kt

package com.example.a02handler

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.lang.ref.WeakReference
import java.math.BigInteger
import java.util.*

class MainActivity : AppCompatActivity() {

    private lateinit var show: ImageView
    enum class week{
        星期一,星期二,星期三,星期四,星期五,星期六,星期日
    }

    class MyHandler(private val activity: WeakReference<MainActivity>) : Handler(){
        private var  imageIds = intArrayOf(R.drawable.java, R.drawable.javaee, R.drawable.ajax, R.drawable.android,
                R.drawable.swift)
        private var currentImageId = 0
        override fun handleMessage(msg: Message) {
            if (msg.what == 0x1211){
                activity.get()?.show?.setImageResource(
                        imageIds[currentImageId++ % imageIds.size]
                )
            }
        }
    }

    val  myHandler = MyHandler(WeakReference(this))
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        var result = add(3, 5)
//        test()//函数
//        test1()//类
        test2()//继承
//        test3(); //抽象方法
        test4();

        show = findViewById(R.id.show);
        Timer().schedule(object : TimerTask(){
            override fun run() {
                myHandler.sendEmptyMessage(0x1211)
            }
        }, 0, 1200)
    }

    fun test4() {
        var intman = IntMan()
        println(week.星期二.ordinal)
    }

    /**
     * 抽象方法
     */
    fun test3() {
        var man = Man("男人")
        man.eat()
        man.pee()
        var man2 = Man("男人2")
        man2.eat()
        man2.pee()
        var man3 = Man("男人3")
        man3.eat()
        man3.pee()
        var woman = Wonman("女人")
        woman.eat()
        woman.pee()
        var woman2 = Wonman("女人2")
        woman2.eat()
        woman2.pee()
        var woman3 = Wonman("女人3")
        woman3.eat()
        woman3.pee()

        var humList = listOf<Human>(man, man2, man3, woman, woman2, woman3)
        for (h in humList) {
            h.pee()
        }
    }

    /**
     * 继承
     */
    fun test2() {
        var son = Son()
        println("son's charator ${son.chactor}")
        son.action()
    }

    /**
     * 对象
     */
    fun test1() {
        var result = fact(BigInteger("100"))
        var react = React(20, 10)
        println("gao===:${react.height}")
        println("kuan===:${react.width}")
        react.closeDoor()
        react.selecteMode(1)
        react.start()
//        println("result=====" + result)
    }

    private fun fact(num:BigInteger):BigInteger{
        return if (num == BigInteger.ONE) {
            BigInteger.ONE
        } else{
            num*fact(num - BigInteger.ONE)
        }
    }

    private fun add(a: Int, y: Int) : Int = a + y

    private fun test() {
        val a: Int = 10000
        println("???" + (a === a)) // true,值相等,对象地址相等

        //经过了装箱,创建了两个不同的对象
        val boxedA: Int? = a
        val anotherBoxedA: Int? = a

        //虽然经过了装箱,但是值是相等的,都是10000
        println(boxedA === anotherBoxedA) //  false,值相等,对象地址不一样
        println(boxedA == anotherBoxedA) // true,值相等
    }


}

Son.kt

package com.example.a02handler

class Son : Father(){
    override fun action() {
        println("son action===")
    }
}

Father.kt

package com.example.a02handler

open class Father {
    var chactor:String = "father==="
    open fun action() {
        println("father action=====")
    }
}

输出:

I/System.out: son's charator father===
    son action===
    1

 

本文地址:https://blog.csdn.net/augfun/article/details/108722907

相关标签: Kotlin