简书链接:泛型和继承演示
文章字数:19,阅读全文大约需要1分钟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

open abstract class ParentTest<in T> {
abstract fun hello(other: T): Int
}

open abstract class MyAB<T> : ParentTest<T>() {


}

class MyImp1 : MyAB<Double>() {
override fun hello(other: Double): Int {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
println("hello,you call ")
return 1;
}


}


fun demo(x: ParentTest<Double>) {
x.hello(1.0) // 1.0 has type Double, which is a subtype of Number
// Thus, we can assign x to a variable of type ParentTest<Double>
val y: ParentTest<Double> = x // OK!
}

/*
fun demo1(x: ParentTest<Int>) {
x.hello(1.0) // 1.0 has type Double, which is a subtype of Number
// Thus, we can assign x to a variable of type ParentTest<Double>
val y: ParentTest<Double> = x // OK!
}
*/

demo(MyImp1());
// demo1(MyImp1());//err musct int