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
推荐阅读
-
自定义一个theme在不同的sdk环境下继承不同的值
-
对python中类的继承与方法重写介绍
-
Python实现类继承实例
-
python基础知识(安装、数据类型、list列表、字典、函数、类、继承、文件操作)
-
《从零开始学Swift》学习笔记(Day 41)——类的继承
-
《从零开始学Swift》学习笔记(Day43)——构造函数继承
-
PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】
-
给Asp.Net初学者的关于继承和多态性的例子
-
解析:继承ViewGroup后的子类如何重写onMeasure方法
-
跟老齐学Python之编写类之四再论继承