package com.av.bcacc.kotlinpai.api.basic.study
import kotlin.reflect.KProperty
class KotlinGeneric {
var p by Delegate()
fun deal(){
p = 123
println("代理方法p= "+p)
}
}
class MyClass<T> {
fun deal(param: T): T {
return param
}
}
class MyClass2 {
fun <T> deal(param: T): T {
return param;
}
fun <T : Number> deal2(param: T): T {
return param
}
}
class MySet<T>(val helperSet: HashSet<T>) : Set<T> {
override val size: Int
get() = helperSet.size
override fun contains(element: T): Boolean {
return helperSet.contains(element)
}
override fun containsAll(elements: Collection<T>): Boolean {
return helperSet.containsAll(elements)
}
override fun isEmpty(): Boolean {
return helperSet.isEmpty()
}
override fun iterator(): Iterator<T> {
return helperSet.iterator()
}
}
class MySetNew<T>(var helperSet: HashSet<T>) : Set<T> by helperSet {
override fun isEmpty(): Boolean {
return false;
}
}
class Delegate {
var propValue: Any? = null
operator fun getValue(kotlinGeneric: KotlinGeneric, prop: KProperty<*>): Any? {
return propValue
}
operator fun setValue(kotlinGeneric: KotlinGeneric, prop: KProperty<*>,value: Any?) {
propValue = false
}
}
fun main() {
println("hello kotlin")
println("##########################Kotlin 泛型类的使用####################")
var myClass = MyClass<Int>()
println("myClass deal result:" + myClass.deal(123))
var myClass2 = MyClass2()
println("myClass2 deal result:" + myClass2.deal("hello"))
println("调用指定类型的方法myClass2 deal result:" + myClass2.deal2(567))
println("##########################Kotlin 委托的使用####################")
var mKotlinGeneric = KotlinGeneric()
mKotlinGeneric.deal()
}