Kotlin 泛型

1.官方文档

  英文:https://kotlinlang.org/docs/reference/generics.html

  中文:https://www.kotlincn.net/docs/reference/generics.html

2.泛型与类

2.1 简单泛型类

 1 //类模板
 2 fun test_template1() {
 3 
 4     class Student(val n : String){ override fun toString(): String { return "student.name = $n" } }
 5 
 6     //简单类模板
 7     open class A <T> (var t : T){ override fun toString(): String { return "t = $t" } }
 8 
 9     A(1).let                    { Log.e(TAG_TEMPLATE,"A <Int>     : $it")       }
10 
11     A("string template").let    { Log.e(TAG_TEMPLATE,"A <String>  : $it")       }
12 
13     A<Float>(1314.0f).let       { Log.e(TAG_TEMPLATE,"A <Float>   : $it")       }
14 
15     A(Student("li3")).let       { Log.e(TAG_TEMPLATE,"A <Student> : $it")       }
16 
17 }

  结果

A <Int>     : t = 1
A <String>  : t = string template
A <Float>   : t = 1314.0
A <Student> : t = student.name = li3

2.2 泛型显示声明

public class ExplicitTypeSpecification {
    private <K, V> Map<K, V> map() {
        return new HashMap<K, V>();
    }    

    private static <K, V> Map<K, V> static_map() {
        return new HashMap<K, V>();
    }    

    static void f(Map<String, List<Integer>> map) { }    

    void g(Map<String, List<Integer>> map) { }    
    

    public void nonStaticMethod() {
        // The method g(Map<String,List<Integer>>) in the type ExplicitTypeSpecification 
        // is not applicable for the arguments (Map<Object,Object>)
        //! g(map());
        g(this.<String, List<Integer>>map());
    }    

    public static void staticMethod() {
        f(ExplicitTypeSpecification.<String, List<Integer>>static_map());
    }
}

2.3 泛型类与继承

 1 interface TIF <T>
 2 interface TIF2 <E> : TIF <Int>  //接口模板继承某个特定类型
 3 interface TIF3 <U> : TIF <U>    //接口模板继承接口模板
 4 
 5 fun test_template2(){
 6 
 7 
 8     class A     : TIF<Int>      //实现接口模板
 9 
10     open class  B <T> ()
11     class C     : B<Int>()      //普通类继承类模板
12     class D<N>  : B<N>()        //类模板继承类模板
13 
14 
15     open class  E <T,U>         //多个参数类模板
16     class F     : E <Int,Float>()
17     class G <T> : E <T,Int>()
18 
19 }

3.泛型函数

3.1 简单泛型函数

 1     //1.简单函数模板
 2     class Student(val name : String = ""){ override fun toString(): String { return "name = $name" } }
 3 
 4     fun <T> fun1( t : T ) {
 5         Log.e(TAG_TEMPLATE,"test_template3 t : $t")
 6     }
 7 
 8     fun1(1)
 9     fun1(1.0f)
10     fun1(Student("li3 "))

  结果

test_template3 t : 1
test_template3 t : 1.0
test_template3 t : name = li3  

3.2  不支持默认实参

fun <T> fun2( t : T = Int ) { }  //不支持默认实参

3.3 能返回泛型类

1     //3.函数参数、返回值 用类模板
2     class A <T> (var t : T ) { override fun toString(): String { return "t = $t" } }
3     fun <T> fun3(t : T) : A <T>{
4         return A(t)
5     }
6     fun3(1).let {  Log.e(TAG_TEMPLATE,"test_template3 it: $it") }

  结果

test_template3 it: t = 1

4.泛型类型的协变

4.1 java中泛型的协变

  Java 中为保证运行时的安全,泛型是不协变的, List<String> 并不是 List<Object> 的子类型,使用类型通配符解决这个问题。如

1 // Java
2 interface Collection<E> …… {
3   void addAll(Collection<? extends E> items);
4 }

  这时 Collection<String>  可看作 Collection<? extends Object>的子类型,E是协变的,items可接收E的类型,但是item不能调用与类型有关的修改item的api,item无法确定它的子类型到底有哪些api。

  • 调用 item.add(), item.set() 不可以,因为add()与set()都有参数
  • 调用 item.clear(),可以,clear()无参数。

#.reified

如:

public inline fun <reified T : Enum<T>> enumValueOf(name: String): Tpublic inline fun <reified T : Enum<T>> enumValueOf(name: String): T

#代码

  https://github.com/f9q/kotlin

 

posted @ 2019-11-26 22:42  f9q  阅读(164)  评论(0编辑  收藏  举报