Detailed explanation of kotlin’s enumeration and exception examples

1、 Definition of enumeration in kotlin

Enum requires two keywords enum class, for example

enum class Color(val r: Int,val g: Int,val b: Int){
 //彩虹色也是一个典故:韦克菲尔德战役
 RED(255,0),ORANGE(255,165,YELLOW(255,255,GREEN(0,BLUE(0,255),INDIGO(75,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

Preliminary use

//枚举和when的配合使用
fun getMnemonic(color: Color): String{

 //when配合枚举使用
 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 results of multiple cases are the same, they can be connected by commas, such as

//when的多个case同一个结果的方式
fun getWarmth(color: Color) = when(color){
 Color.RED,Color.ORANGE,Color.YELLOW -> "warm"
 Color.GREEN -> "neutral"
 Color.BLUE,Color.INDIGO,Color.VIOLET -> "cold"
}

In case of other situations than case, use else. 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 set

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 check 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 anomalies and

One drawback of the detected exception is that we don't need to catch the exception in many cases, because we can't deal with it.

For example, bufferreader.close may throw IOException exception, but many programs will not take meaningful action on this exception, so the code written for capturing this exception is redundant code

Of course, its use is basically the same as Java, try catch or try catch finally blocks

//将读取到的字符串类型转化成Int类型
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)
}

summary

Learning kotlin is not only learning a new language, but also learning to change your habitual way of thinking

Compared with Java, using kotin brings you different thinking habits

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>