Explanation of Enumeration and Exception Example of Kotlin
- 2021-11-10 10:56:48
- OfStack
1. Definition of enumeration in kotlin
Enumeration requires two keywords enum class, such as this
enum class Color(val r: Int,val g: Int,val b: Int){
// So is the rainbow color 1 Allusions: The Battle of Wakefield
RED(255,0,0),ORANGE(255,165,0),YELLOW(255,255,0),
GREEN(0,255,0),BLUE(0,0,255),INDIGO(75,0,130),VIOLET(238,130,238);
fun rgb() = (r * 255 + g) * 256 + b
}
Call the rgb function in the enumeration
fun main() {
println("RED's RGB value is ${Color.RED.rgb()}")
2. Combination of enumeration and when
Initial use
// Enumeration and when Matching use of
fun getMnemonic(color: Color): String{
//when Use with enumeration
return when(color){
Color.RED -> "Richard"
Color.ORANGE -> "Of"
Color.YELLOW -> "York"
Color.GREEN -> "Gave"
Color.BLUE -> "Battle"
Color.INDIGO -> "In"
Color.VIOLET -> "Vain!"
}
}
If the result of multiple case is 1, it can be connected by commas, such as
//when Multiple of case Same as 1 The way of a result
fun getWarmth(color: Color) = when(color){
Color.RED,Color.ORANGE,Color.YELLOW -> "warm"
Color.GREEN -> "neutral"
Color.BLUE,Color.INDIGO,Color.VIOLET -> "cold"
}
Use else in cases other than case. Replace if with when
fun mix(c1: Color,c2: Color) =
when(setOf(c1,c2)){
setOf(Color.RED,Color.YELLOW) -> Color.ORANGE
else -> throw Exception("Dirty Color")
}
Use when without parameters
fun mixOptimized(c1: Color,c2: Color) =
when{
(c1 == Color.RED&& c2 == Color.YELLOW ||
c2 == Color.RED&& c1 == Color.YELLOW) -> Color.ORANGE
else -> throw Exception("Dirty Color")
}
setOf is to add elements to the Set collection
Type can be judged by is in when
fun eval(e: Expr): Int =
when(e){
is Num -> e.value
is Sum -> eval(e.right) + eval(e.left)
else -> throw IllegalArgumentException("Unknown expression")
}
Use in to check the range in when
fun recognize(c: Char) = when(c){
in '0'..'9' -> "It's a digit!"
in 'a'..'z',in 'A'..'Z' -> "It's a letter"
else -> "I don't know what it is."
}
3. Exceptions in Kotlin
kotlin does not distinguish between detected exceptions and
Exceptions in Java: Checked exceptions that must be handled explicitly
Exceptions in Kotlin: There is no distinction between detected and undetected exceptions. You do not specify the exception thrown by the function, and you can or can not handle the exception.
Checked exceptions have a drawback: In many cases, we don't need to catch exceptions, because we can't handle them if we catch them.
For example, BufferReader. close may throw an IOException exception, but many programs will not take meaningful action on this exception, so the code written to catch this exception is redundant code
Of course, its use is basically the same as Java, try-catch or try-catch-finally block
// Converts the read string type to Int Type
fun readNumber(reader: BufferedReader): Int?{
try {
val line = reader.readLine()
return Integer.parseInt(line)
}catch (e: NumberFormatException){
return null
}finally {
reader.close()
}
}
In fact, the try keyword in Kotin is also an expression, so it can also be written as follows:
fun readNumber2(reader: BufferedReader){
val number = try {
val line = reader.readLine()
Integer.parseInt(line)
}catch (e: NumberFormatException) {
return
}
println(number)
}
Summarize
Learning Kotlin is not only learning a new language, but also learning to change the way you think
Compared with Java, using Kotin brings you different thinking habits
The familiar if is now an expression with a return value The when expression is similar to the switch in Java but more powerful The for loop is more convenient in kotlin, especially when subscripts are required for iteration of map and iteration sets In Kotlin, you can create 1 interval by = =.. = =. Intervals and sequences allow the use of Uniform 1 syntax and the same set of abstraction mechanisms in the for loop, and you can also use = = in = = and = =! in== to check whether the value belongs to an interval. Kotlin does not distinguish between detected and undetected exceptions. Discard the meaningless re-throwing and ignoring exception code in Java. Medium