当前位置:网站首页>Kotlin advanced generic
Kotlin advanced generic
2022-06-25 09:52:00 【seevc】
stay Android We often use generics in development , Such as :List、Map、Set、Adapter etc. , So in Kotlin Generics are also supported in .
What is generics ?
Generic : Postpone type specific work until the object is created or the method is called .
One 、Kotlin Define generic methods in
stay Kotlin Generic types are defined in Java equally , There are two ways :
- Defined on class
- Defined in a function
Define examples on classes :
class MagicBox<T>(val item: T) {
var available = false
fun fetch(): T? {
return item.takeIf { available }
}
}
Defined in a function
class MagicBox<T>(val item: T) {
var available = false
/**
* Add normal form type to function , similar Java
*/
fun <R> fetch(anotherGenericity: (T) -> R): R? {
return anotherGenericity(item).takeIf { available }
}
}
Two 、Kotlin Define constraint generics in
Kotlin Defines constraint generics and Java < T extend XXX> Generics are defined in a similar way , Indicates that the type specified by a generic type must be a class of the specified type or a subclass that inherits the specified type class . This sentence is a little convoluted , for instance : Define a generic type class A<T : B>(), Generics passed in T It has to be for B perhaps B Subclasses of . The complete example is as follows :
// Define a constraint generic
private class ConstraintMagicBox<T:Human>(item:T){
}
// Define a parent class
private open class Human(val name: String,val age:Int)
// Define a Human Subclasses of classes
private class Male(name: String,age: Int):Human(name, age)
// Define a Human Subclasses of classes
private class Female(name: String,age: Int):Human(name, age)
fun main() {
// A fellow Human Type can be passed in
val male = ConstraintMagicBox(Male("Jack", 20))
val female = ConstraintMagicBox(Female("Jim", 20))
}
3、 ... and 、Kotlin Define variable number parameter generics
This is similar to Java Define variable parameters for methods in (private void method(int.. num)), stay Kotlin The way to define in depends on keywords **vararg**, Both normal functions and constructors can use .
Examples are as follows :
private class VarMagicBox<T : VarHuman>(vararg val items: T) {
// From items Get data in , among items Type not Array
fun fetch(index: Int): T? {
return items.getOrNull(0)
}
// Add variable arguments to the function
fun fetch(vararg indexs: Int):List<T>{
indexs.takeIf {
indexs.isNotEmpty()
}.run {
return items.filterIndexed { index, t ->
indexs.contains(index)
}
}
}
}
private open class VarHuman(val name: String, val age: Int)
Four 、Kotlin of use in、out Modifying generics
stay Java Define a List in , Specify a specific type , When creating an instance, you can only new Examples of this type , unable new Create an instance of a subclass or parent class ,
Example :ArrayList<String> list = new ArrayList<CharSequence>()
perhaps ArrayList<CharSequence> list = new ArrayList<String>()
These two ways of writing are in JAVA Is not supported , The correct way to write is :ArrayList<String> list = new ArrayList<String>().
But in Kotlin Can support .
Of course, the generic example defined above does not support , that Kotlin How can we support this way of writing ?
because Kotlin There are two important keywords in in( Covariance )、out( Inversion )
Let's take a look at how the following two keywords can support the above writing .
Very simple to use , Let's start with an example , Take a look at the usage :
//out Decorated generics Only use generics as function return values
// effect : Let subclass generic objects assign values to parent generic objects
interface OutTest<out T>{
fun outTest():T
}
//in Decorated generics Only use generics as function arguments , A generic cannot be treated as a return value
// effect : The parent generic object can be assigned to the child generic object
interface InTest<in T>{
fun inTest(param : T)
}
The test code is as follows :
open class Food()
open class FastFood():Food()
class Hamburg():FastFood()
class FastFoodStore() : OutTest<FastFood>{
override fun outTest(): FastFood {
println("FastFoodStore ----------")
return FastFood()
}
}
class HamburgStore():InTest<FastFood>{
override fun inTest(param: FastFood) {
println("HamburgStore-----------")
}
}
fun main() {
// Subclass objects can be passed to parent generic objects out
val food1 : OutTest<Food> = FastFoodStore()
// A parent class object can be passed to a child class generic object in
val food2 : InTest<Hamburg> = HamburgStore()
}
keyword in、out Use summary
There are two main points :
outDecorated generics can only be used inFunction return valueUse in ,inDecorated generics can only be used inThe parameters of the functionUse in ;outDecorated generics can onlyThe subclass generic object is assigned to the parent generic object,inDecorated generics can onlyThe parent class generic object is assigned to the child class generic object, As shown in the figure below ;
Welcome to leave a message for us to exchange and learn from each other !
Sample source address kotlin_demo
边栏推荐
- ‘Flutter/Flutter. h‘ file not found
- 2021mathorcupc topic optimal design of heat dissipation for submarine data center
- [matlab] image binarization (imbinarize function)
- What should be paid attention to in PMP examination?
- Notes on writing questions in C language -- monkeys eat peaches
- 在指南针上面开股票账户好不好,安不安全?
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
- 2022 meisai question a idea sharing
- Vscode attempted to write the procedure to a pipeline that does not exist
- 2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
猜你喜欢

CyCa children's physical etiquette Yueqing City training results assessment successfully concluded

Remove the mosaic, there's a way, attached with the running tutorial

Register the jar package as a service to realize automatic startup after startup

Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021

Wearable devices may reveal personal privacy

链表 删除链表中的节点
![[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition](/img/b4/84c30ed112c4dffd8d51697b2f4a4f.jpg)
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition

22 mathematical modeling contest 22 contest C

Creo makes a mobius belt in the simplest way

Jetpack compose layout (III) - custom layout
随机推荐
瑞吉外卖项目(二)
How to "transform" small and micro businesses (II)?
8、智慧交通项目(1)
How to make a self-made installer and package the program to generate an installer
Neat Syntax Design of an ETL Language (Part 2)
使用EVO
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
[buuctf.reverse] 117-120
8. Intelligent transportation project (1)
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
vscode试图过程写入管道不存在
[buuctf.reverse] 121-125
[learn C from me and master the key to programming] insertion sort of eight sorts
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
I put a two-dimensional code with rainbow candy
puzzle(019.2)六边锁
Download the arm64 package of Debian on X86 computer
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most
What functions should smart agriculture applet system design have
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate