当前位置:网站首页>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 :
out
Decorated generics can only be used inFunction return value
Use in ,in
Decorated generics can only be used inThe parameters of the function
Use in ;out
Decorated generics can onlyThe subclass generic object is assigned to the parent generic object
,in
Decorated 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
边栏推荐
- 可穿戴设备或将会泄露个人隐私
- CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
- Encoding format for x86
- Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
- Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
- 测试开发工程师
- Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
- CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
- JS tool function, self encapsulating a throttling function
- Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
猜你喜欢
Encoding format for x86
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
Prediction of pumpkin price based on BP neural network
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
[buuctf.reverse] 117-120
Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points
汇付国际为跨境电商赋能:做合规的跨境支付平台!
How to delete a blank page that cannot be deleted in word
随机推荐
TLAB mechanism of JVM object memory allocation and TLAB process in G1
22 mathematical modeling contest 22 contest C
Creo makes a mobius belt in the simplest way
pmp考试题型需要注意哪些?
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function
Force buckle -104 Maximum depth of binary tree
puzzle(019.2)六边锁
[IOU] intersection over union
Neo4jdesktop (neo4j desktop version) configures auto start (boot auto start)
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
Exception: gradle task assemblydebug failed with exit code 1
Is it safe to open an account on the compass?
Is it safe to open an account with Great Wall Securities by mobile phone?
js工具函数,自己封装一个节流函数
Reasons for Meiye to choose membership system
Processing picture class library
[buuctf.reverse] 117-120
【mysql学习笔记21】存储引擎
Mysql 源码阅读(二)登录连接调试
With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire