当前位置:网站首页>Kotlin's learning journey

Kotlin's learning journey

2020-12-07 12:40:34 osc_ 3vp99iw6

One 、 Type the alias typealias Introduce

Kotlin Similar to C Linguistic typedef The function of : You can specify a more readable name for an existing type .Kotlin Provides typealias To define type aliases .

typealias The syntax of the statement is :

typealias  Type the alias  =  Existing types 

If the type name is too long , You can also introduce a shorter name , And replace the original type name with a new name .

  • It helps shorten longer generic types . for example , In general, reducing set types is attractive :
//  by Set<Network.Node>  Specify a shorter alias NodeSet 
typealias NodeSet = Set<Network.Node>
//  by MutableMap<K, MutableList<File>>  Specify a shorter alias FileTable<K>
typealias FileTable<K> = MutableMap<K, MutableList<File>>

Then you can use it directly NodeSet and FileTable Named variable .

var set : NodeSet 
var table : FileTable<String> 
  • You can provide another alias for the function type :
typealias MyHandler = (Int, String, Any) -> Unit

typealias Predicate<T> = (T) -> Boolean
  • You can create new names for inner classes and nested classes :
    A lot of times , We can also give inner classes a shorter name by defining aliases . For example, the following procedure .
class A {
   
   
    inner class Inner
}
class B {
   
   
    inner class Inner
}
// by A.Inner The inner class knows the alias 
typealias AInner = A.Inner
// by B.Inner The inner class knows the alias 
typealias BInner = B.Inner

fun typealiasTest(){
   
   
    //  Use AInner  Defining variables 、 Call object 
    var a : AInner = A().AInner()
    //  Use BInner  Defining variables 、 Call object 
    var b = B().BInner()
}

 Insert picture description here

Two 、Kotlin The alias function used by itself

Kotlin It makes a lot of use of the alias function . such as Kotlin Using aliases to create Kotlin Classes and Java Relationships between classes .

The following code is Kotlin An alias defined in the set system .

// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

package kotlin.collections

@kotlin.SinceKotlin public typealias ArrayList<E> = java.util.ArrayList<E>

@kotlin.SinceKotlin public typealias HashMap<K, V> = java.util.HashMap<K, V>

@kotlin.SinceKotlin public typealias HashSet<E> = java.util.HashSet<E>

@kotlin.SinceKotlin public typealias LinkedHashMap<K, V> = java.util.LinkedHashMap<K, V>

@kotlin.SinceKotlin public typealias LinkedHashSet<E> = java.util.LinkedHashSet<E>

@kotlin.SinceKotlin public typealias RandomAccess = java.util.RandomAccess

 Insert picture description here

3、 ... and 、Lambda Expression alias

And Java comparison ,Kotlin Of Lambda Expression is a characteristic function with great difference :

Java Of Lambda The type of expression is a functional interface , and Kotlin Of Lambda Between the types of expressions are function types .

therefore Kotlin Also allowed for Lambda The type of the expression specifies the alias . As shown in the following code :

// by  (T) -> Boolean  Type specifies the alias   Predicate<T>
typealias Predicate<T> = (T) -> Boolean

fun foo(p: Predicate<Int>) = p(42)

fun main() {
   
   
    val f: (Int) -> Boolean = {
   
    it > 0 }
    println(foo(f)) //  Output  "true"
    
    // Use  Predicate<Int>  Defining variables , The value of this variable is a Lambda expression 
    val p: Predicate<Int> = {
   
    it > 0 }
    
    //  by filter()  Methods the incoming p Parameters , Keep only greater than 0 The number of 
    println(listOf(1, -2, 4, 5, 6, 7 ).filter(p)) //  Output  "[1]"
    
    
    
    // Use  Predicate<String>  Defining variables , The value of this variable is a Lambda expression 
    val p2: Predicate<String> = {
   
    it.length > 4 }
    
    //  by filter()  Methods the incoming p Parameters , Only the length is greater than 4 String 
    println(arrayOf("Java", "Kotlin", "Python", "C", "C++","OuyangPeng").filter(p2))
    
}

The running result is :

true
[1, 4, 5, 6, 7]
[Kotlin, Python, OuyangPeng]

 Insert picture description here

版权声明
本文为[osc_ 3vp99iw6]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207112703758n.html