Kotlin and kotlin

Source: Internet
Author: User
Tags throw exception

Kotlin and kotlin
The main function of the basic function variable can be independent of the class static function.
Class static function, you need to put the function into the braces of the companion object.

class Static {    companion object {       fun add( a: Int,  b:Int):Int =  (             a + b               )    }}fun main(args: Array
 
  ) {    Static.add(1,2);}
 
String Template
fun main(args: Array
 
  ) {    println("Hello, ${if (args.size > 0) args[0] else "someone"}!")}
 
The constant uses val, and the variable uses the var class and the property-defined iterator.
Class Rectangle (val height: Int, val width: Int) {val isSquare: Boolean get () {// return height = width }}
Kotlin source code Layout
Kotlin does not distinguish between the import class and function. You can directly import the method, similar to the usage of python and ReactJs. Multiple classes can be placed in the same file and any name can be selected for the file, which weakens the concept enumeration of the package.

Enumeration is very similar to java. You can define parameters by yourself.

Enum class Color (val r: Int, val g: Int, val B: Int) {// 1 declares the attribute RED (255, 0, 9) of the enumerated constant ), ORANGE (255,255, 0), // 2 when each constant is created, specify the attribute values YELLOW (255,255, 0), GREEN (0,255, 0), BLUE (0, 0,255 ), INDIGO (75, 0,130), VIOLET (238,130,238); // 3 semicolon (;) Here is a required fun rgb () = (r * 256 + g) * 256 + B // 4 defines a method in the enumeration class

The difference is:
1. enum class to identify as Enumeration
2. constants in the enumeration must be separated by semicolons. In kotlin, unique constants must be separated.

When

When processing enumeration, when is somewhat similar to the switch-case of java, but you can directly use expression functions in kotlin.

With Parameters
Fun getMnmonic (color: Color) = // 1 directly returns a when expression when (color) {// 2 if the Color is equal to the enumerated constant, returns the corresponding string color. RED-> "Richard" Color. ORANGE-> "Of" Color. YELLOW-> "York" Color. GREEN-> "Grave" Color. BLUE-> "Battle" Color. INDIGO-> "In" Color. VIOLET-> "Vain" else-> throw Exception ("Dirty color") // 3 if no branch is matched, execute this statement}

If multiple conditions meet the same returned result, you can:

Without Parameters

For multiple parameters, you need to write parameters each time. There will be a lot of remainder. You can directly ignore the parameters.

When {// The when (c1 = RED & c2 = YELLOW) without parameters | (c1 = YELLOW & c2 = RED) -> ORANGE (c1 = YELLOW & c2 = BLUE) | (c1 = BLUE & c2 = YELLOW) -> GREEN (c1 = BLUE & c2 = VIOLET) | (c1 = VIOLET & c2 = BLUE) -> INDIGO else-> throw Exception ("Dirty color ")}
Intelligent type conversion

In java, when calling an object of another type, if it is an object, we need to convert it to the corresponding type before processing, but it is not required in kotlin, after determining a type, you can directly call the attributes of this class.

Interface Exprclass Num (val value: Int): Expr // 1 class Sum (val left: Expr, val right: Expr): Expr // 2 the parameters for the Sum operation can be any Expr: Num object or other Sum object fun eval (e: Expr ): int {if (e is Num) {val n = e as Num // 1 explicit Num type conversion is redundant return e. value} if (e is Sum) {return eval (e. right) + eval (e. left) // 2 variable e is smart type conversion} throw IllegalArgumentException ("Unknown expression")} fun main (args: Array
 
  
) {Println (eval (Sum (Num (1), Num (2), Num (4 ))))}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.