Kotlin piecemeal knowledge collection

6, Java and kotlin code conversion

6.1, java-> kotlin 4 methods:

1. ctrl + shift + a shortcut key to enter Convert Java File to Kotlin File
2. ctrl + alt + shift + k Convert Java code to Kotlin
3. Open the java file to be converted, and then select Code-> Convert Java File to Kotlin File (at the bottom)
4. Move the mouse to any file location in the java directory, right-click and select new-> Kotlin File / class (it will create a .kt file in the java directory by default), and finally the java to be converted Copy the code to the .kt file and the conversion will be completed automatically;

6.2、kotlin -->java:

First Tools> Kotlin> Show Kotlin Bytecode, then click Decompile.

5. Data type

https://www.kotlincn.net/docs/reference/basic-types.html

The built-in types related to numbers in Kotlin are: byte, short, Int, Long, Float, Double.

Note:

  • There is no automatic upward transformation, such as Int to Long, you need to adjust the toXxx method yourself;
  • Long type must end with uppercase L, not lowercase, such as 1024L
  • The character Char is not a Number and is declared with single quotes, such as 'c'. It cannot be used directly as a number like Java. If you want to give the value of Char to Int, you need to call toInt ()
  • Boolean value is true or false
  • Kotlin does not support octal, hexadecimal 0x, binary 0b;
  • Bitwise operators, and and or operators in Java: | and &, or and and keywords in Kotlin to replace other operators There are also separate keyword substitutions: shl (signed left), shr (signed right) Shift), ushr (unsigned right shift), xor (bitwise XOR), inv (bitwise inverse)

4. Use of the label @

There are 3 expressions in Kotlin that jump out of the program flow:

  • return. The default behavior is to return from the innermost function or anonymous function.
  • break. End the innermost loop.
  • continue. In the innermost loop, jump to the next loop.

Labels:
Any expression in Kotlin can be labeled with a label label; the
label consists of an identifier followed by an @ symbol, for example: abc @, return @ are all legal labels (see label syntax).

loop@ for (i in 1..100) {
    for (j in 1..100) {
        if (...) break@loop
    }
}

We added the loop @ tag at the beginning of the lambda expression, which is equivalent to the instruction entry address that records the lambda expression. Then jump to the address of the lambda expression via @loop expression.

Implicit label:
The name of the implicit label is the same as the name of the function to which the Lambda expression is passed.
Use tags to control return goals:return@

btn_login.setOnClickListener({
  val phone = et_phone.text.toString().trim()
   if (TextUtils.isEmpty(phone)) {
       ToastUtils.showShortToast(this, "手机号不能为空")
       return@setOnClickListener //从lambda表达式@setOnClickListener中返回。
   }
	...
})

Back and jump

https://www.jianshu.com/p/0b88c4e50768

3、Getters 与 Setters

The complete syntax for declaring an attribute is

var <propertyName>[: <PropertyType>] [= <property_initializer>]
    [<getter>]
    [<setter>]

The initializer, getter, and setter are all optional. If the attribute type can be inferred from the initializer (or return value from its getter, as shown below), it can also be omitted.

2、getClass()

To get the Java class of an object, use the java extended attribute on the class reference:

ClassName::class.java
例如:val fooClass = Foo::class.java

The above code uses bound class references that have been supported since Kotlin 1.1. You can also use javaClass extended attributes:

instance.javaClass
For example: val fooClass = foo.javaClass

1. Activity Jump

Method one (by class):

 val intent = Intent() 
 intent.setClass(this,Main2Activity::class.java)
 startActivity(intent)

Method two (via Action): The
two activities belong to different modules and have no dependencies.

val intent = Intent() 
intent.setAction("XXXXXXX")
startActivity(intent)
Published 45 original articles · Like 24 · Visits 50,000+

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/90476629