Kotlin泛型简介
在Kotlin中,泛型是一个允许定义类、方法和属性的功能,这些类、方法和属性用于访问不同的类型,可以在编译时检查所有类、方法的差异,泛型类或方法被声明为参数化的类型,也是具有实际参数类型的泛型的实例。它可以使用像'<>'这样的角括号来声明,泛型主要用于集合中,它只持有单一类型的对象,不接受多个对象。
语法。
kotlin语言有许多默认的关键字、变量和其他功能,用于实现基于移动的应用程序。就像科特林泛型是其中的一个关键字,它被表示为钻石符号<>,用于声明和利用它在应用程序中。
fun main(args:Array){ var vars=first(parameters) ---some logic codes depends on the requirement--- } class first(parameters){ ----some logic codes--- }
上述代码是实现泛型的基本语法,它被定义为模板,并由kotlin编译器动态确定,因此数据类型被传递为泛型,同样的数据类型将被调用到主方法中。
泛型如何在Kotlin中工作?
在Kotlin中,一个类和一个类型被使用,它们在该包中需要的任何地方被调用,同样的东西将作为参数传递给其他类的方法。在Kotlin中提供了更高层次的变量类型,它被称为泛型类型,它是一种类型安全,只允许持有单一类型的对象,不允许存储其他对象。它与其他功能一样,除了不需要类型化之外,泛型代码在编译时被检查,因此在运行时可以避免任何问题。当我们想把泛型分配给它的任何一个超级类型时,我们可以使用 "out "这样的关键字,这样我们就可以把其他的泛型分配给它的任何一个子类型,我们需要使用 "in "这样的关键字来指代参数的输入类型。它有Reified类型参数,允许你在运行时引用特定的类型作为内联函数调用的类型参数,它不像普通的类函数那样,因为参数类型在运行时被抹去。
Kotlin泛型的例子
以下是一些例子。
例子#1
代码。
import java.util.Scanner class First(id: T){ var id: T = id init { this.id= id println(id) } } fun demos(lst: ArrayList){ for(elm in lst){ println(elm) } } fun examp1(sr1: String,sr2: String, exmp: (String,String) -> String): Unit { val out = exmp(sr1,sr2) println(out) } fun ArrayList.filterOnCondition(condition: (T) -> Boolean): ArrayList{ val rst = arrayListOf() for (itm in this){ if (condition(itm)){ rst.add(itm) } } return rst } fun main(args: Array){ println("Welcome To My Domain its the first example related to the kotlin generics") var idInt: First = First(41) var idString: First = First("41") println(idString) val strlst: ArrayList = arrayListOf("Siva","Raman","Sivarman","Arun","Kumar","Arun kumar") val s: String = strlst[0] println("Thank you users your input string is: $s") demos(strlst) val st1: ArrayList = arrayListOf(23.6f,15.2f,33.7f,44.6f,54.6f) demos(st1) try { val inps = Scanner(System.`in`) println("Welcome To My Domain its the first example that related to the kotlin higher oder function") println("Please enter your inputs") var id1 = inps.nextInt() println("Your input id is "+id1) val la: (Int) -> Unit= {sts: Int -> println(sts) } val inpr = 25 / 5 println(inpr) } catch (e: NullPointerException) { println(e) } finally { println("finally block always executed whenever try is executing") } println("Have in Nice Day users please try again") val exmp:(String,String)->String={st1,st2->"$st1 assigned and it will be automatically goes to $st2"} examp1("First Input","Second Input",exmp) var lst1 = arrayListOf() lst1.add("January is the first month") lst1.add("February is the second month`") lst1.add("March is the third month") lst1.add("April is the fourth month") lst1.add("May is the fifth month") lst1.add("June is the sixth month") lst1.add("July is the seventh month") lst1.add("August is the eigth month") lst1.add("September is the ninth month") lst1.add("October is the tenth month") lst1.add("November is the eleventh month") lst1.add("December is the twelth month") var mfl = lst1.filterOnCondition { it.contains("seven") } val mf2 = { print("Have a Nice day users please continue to spenting your time with us") } frt(mf2) } fun frt(mf2: () -> Unit) { mf2.invoke() } fun samp(inp1: Int, inp2: Int,lamb: (Int) -> Unit){ val result = inp1 + inp2 println(result) }
输出。
上面的例子中,我们使用了Kotlin泛型的基础知识,以及用于执行任务的集合和其他定制方法。
例子#2
代码。
fun main(args: Array){ val strlst: ArrayList = arrayListOf("Siva","Raman","Sivaraman","Arun","Kumar","Arunkumar") strlst.printValue() val flist: ArrayList = arrayListOf(33.8f,7.3f,34.7f) flist.printValue() Bank() var bnk= Bank() bnk.insert(43,"SRBank",34521f) println("${bnk.name}") } fun ArrayList.printValue(){ for(elm in this){ println(elm) } } class Bank { var id: Int = 0 var name: String = "" var bnktrnxlimit: Float = 0.toFloat() fun insert(ac: Int,n: String, am: Float ) { id=ac name=n bnktrnxlimit=am println("Bank no: ${id} holder :${name} bnktrnxlimit :${bnktrnxlimit}") } }
输出。
在第二个例子中,我们使用了集合概念和通用概念来存储银行名称的详细信息。
例子 #3
代码。
data class TV(val tvName: String) class tvBrand(val lst: List) { fun tvBrand(): List { return lst.filterIndexed { id, _ -> id % 2 == 1 } } } fun main() { val lststr = listOf("eAirtec", "Samsung", "Vu") val rslt: tvBrand = tvBrand(lststr) println(rslt.tvBrand()) val lstInt = listOf(2, 4, 6, 8, 10, 12) val rstInt = tvBrand(lstInt) println(rstInt.tvBrand()) val tvlst = listOf( TV("eAirtec"), TV("Samsung"), TV("Vu"), TV("Onida"), TV("Airtel") ) var out = tvBrand(tvlst).tvBrand() println(out) }
输出。
在最后一个例子中,我们使用泛型类型创建并列出了tv列表。
结论
在kotlin语言中,我们使用了许多默认的类、方法和变量来实现基于kotlin的移动应用程序。其中,泛型是其中一种类型,它可以用来执行泛型所使用的操作,并在需要的地方调用其他区域的关键字和操作符。
推荐文章
这是一份关于Kotlin泛型的指南。在这里,我们也讨论了定义和泛型在kotlin中如何工作? 以及不同的例子和它的代码实现。你也可以看看下面的文章,以了解更多信息
The postKotlin Genericsappeared first onEDUCBA.