中缀函数
工厂函数
高阶函数
高阶函数
,就是将函数用作参数或返回值的函数。
对象表达式:
对象声明:
定义泛型累些那个,实在类型名之后、主构造函数之前用尖括号括起的大写西姆类型参数指定:
1 | interface Drink<T> { |
1 | abstract class Color<T>(var t: T /*泛型字段*/){ |
类型参数要放在方法名的前面:
1 | fun <T> fromJson(json: String, tClass: Class<T>): T? { |
限定泛型参数的类型
1 | //所传递的参数类型 T 必须满足是 User 的子类或 User 类 |
泛型
型变
java 中的通配符类型参数: ? extends E
通配符<? extends E>表示包括E在内的所有子类,称为协变
通配符<? super E>表示包括E在内的所有父类,称为逆变
协变: 表示包括E在内的所有子类,泛型对象只能读取,称为生产者
逆变: 表示包括E在内的所有父类,泛型对象只能写入,称为消费者
资料:
Sealed classes are used for representing restricted class hierarchies, when a value can have one of the types from a limited set, but cannot have any other type. They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state.
If you are like me, you probably did not understand that on a first read, especially without real-world code samples.
1 | sealed class Fruit() |
The important part of the code above is the sealed
modifier. As mentioned in the sealed class rules, a sealed class can have subclasses but they must all be in the same file or nested inside of the sealed class declaration. However, subclasses of the sealed class subclasses do not have to be in the same file.
1 | // Nested |
The key benefit of using sealed classes comes into play when you use them in a when
expression. If it’s possible to verify that the statement covers all cases, you don’t need to add an *else*
clause to the statement. However, this works only if you use *when*
as an expression (using the result) and not as a statement.
1 | // Invalid |
The piece of code above fails because we MUST implement ALL types. The error thrown says:
1 | When expression must be exhaustive, add necessary 'is Orange' branch or else branch instead |
This just means that we must include all types of Fruit in the when expression. Or use a catch-all else block.
1 | override fun getItemViewType(position: Int): Int { |
There are many ways that sealed classes can be used, but one of my favorites is in a RecyclerView adapter.
Let’s say we want to display a list of fruits in a RecyclerView, where each fruit has its own ViewHolder.
Apple has anAppleViewHolder
and Orange has an OrangeViewHolder
. but we also want to make sure that every new Fruit added has its own ViewHolder explicitly specified, or we throw an exception. We can easily do this with sealed classes.
Show me the code:
1 | sealed class Fruit { |
参考:
https://proandroiddev.com/understanding-kotlin-sealed-classes-65c0adad7015