package com.jmj.myapp.lamdba
import javax.security.auth.callback.Callback
fun main() {
1.test1 {
println(this+1)
}
}
fun <T> T.test1(callback:T.() -> Unit) {
this.callback()
}
package com.jmj.myapp.lamdba
import javax.security.auth.callback.Callback
fun main() {
test(1) {
println(this)
}
}
fun <T> test(re: T, callback:T.() -> Unit) {
re.callback()
}
package com.tiger.mykotlinapp.tongpeifu
fun main() {
//*==Any
val list: List<*> = listOf("123", null, 1)
list.forEach {
println(it is Int)
}
}
package com.tiger.mykotlinapp.whereDemo
fun main() {
MyClass<Prm>(Prm())
test<Prm>()
}
interface A
interface B
interface C
class Prm:A,B,C
//这是 和 三个满足才算
class MyClass<T> (var num:T) where T:A,T:B,T:C {
fun test(num: T){
println("输出:$num")
}
}
fun <T>test() where T:A,T:B,T:C{
}
都是子类泛型变成父类
out是get方法,子类提升父类输出
in是set方法,子类提升父类输入
一般是委托接口和属性的
package com.tiger.mykotlinapp.by
fun main() {
println(HomeService(HomeDaoImpl(),ADaoImpl()).getAllData())
println(HomeService(HomeDaoImpl(),ADaoImpl()).getById(123))
}
interface HomeDao{
fun getAllData():List<String>
}
interface ADao{
fun getById(id:Int):String
}
class HomeDaoImpl:HomeDao{
override fun getAllData(): List<String> {
return listOf("home")
}
}
class ADaoImpl:ADao{
override fun getById(id:Int): String {
return id.toString();
}
}
//将 HomeDaoImpl委托 给 HomeService by后面跟的是实现类的对象 这里只能委托接口,抽象类和普通类委托不了
class HomeService(homeDaoImpl: HomeDaoImpl,aDaoImpl: ADaoImpl):HomeDao by homeDaoImpl,ADao by aDaoImpl{
fun getRedisData():String{
return "redis";
}
}
package com.tiger.mykotlinapp.by
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun main() {
//将属性委托给类 顶层作用域委托
var test:String by MyTestClass()
test="213";
println(test)
}
//test并不属于哪个类 就用nothing
//第一个泛型 是委托属性所属的那个类,第二个泛型是委托属性的类型
class MyTestClass:ReadWriteProperty<Nothing?,String>{
private var myVar="";
override fun getValue(thisRef: Nothing?, property: KProperty<*>): String {
return myVar
}
override fun setValue(thisRef: Nothing?, property: KProperty<*>, value: String) {
this.myVar = value
}
}
package com.tiger.mykotlinapp.by
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun main() {
A().apply {
atest="123";
println(atest)
}
// B().apply {
// atest="asda";
// println(atest)
// }
}
class A{
var atest:String by MyTestClass1()
}
//class B{
// var atest:String by MyTestClass1()
//}
//test并不属于哪个类 就用nothing
//第一个泛型 是委托属性所属的那个类,第二个泛型是委托属性的类型
class MyTestClass1: ReadWriteProperty<A, String> {
private var myVar = ""
override fun getValue(thisRef: A, property: KProperty<*>): String {
println("调用类委托的get")
return myVar
}
override fun setValue(thisRef: A, property: KProperty<*>, value: String) {
println("调用类委托的set")
this.myVar= value
}
}
package com.tiger.mykotlinapp.by
fun main() {
// //延迟委托
// //只能初始化一次。不能重复赋值,所以是val
// val test by lazy{
// println("初始化了Test")
// "hello"
// }
// //线程安全只会初始化一次
// println(test)
// println(test)
val a = Ab()
println(a.aTest)
println(a.aTest)
}
class Ab{
val aTest:Boolean by lazy {
println("1")
println("1")
println("1")
println("1")
println("1")
println("1")
true
}
}
package com.tiger.mykotlinapp.ObeserveWeiTuo
import kotlin.properties.Delegates
fun main() {
var test by Delegates.observable("hello"){_,oldVal,newVal->
//监听到test发生变化后调用的值
println("oldval $oldVal")
println("newVal $newVal")
}
test="tees"
test="test"
println(test)
}
?