Introduction to Kotlin Basics - Static Variables, Constants And Static Functions, Methods

2022 passed by in a flash, and 2023 came quietly. One night when I read the first line of code (third edition), I saw several ways to use static methods in Kotlin. I still have some time to write a new year. Demo simple test record

在 Kotlin 中使用静态的方式不止一种,嗯... 大约有3 - 4种,具体采用哪种方式,还需要自己根据场景选择了~

JavaIn 工具类or 管理常量, we will use staticstatic decoration, mainly for the调用方便

Java basic knowledge

It is mainly divided into 常规使用and 静态使用, which is convenient for beginners to supplement the basics ( os:现在还有新人么...)

normal method

package com.example.ktobject;

public class JavaUtil {
    
    

    public void doSomething() {
    
    
        System.out.println("2023希望我们都更好,加油");
    }
}

Regular call (you need to create the entity class first, and then you can call the internal method)

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        var javaUtil = JavaUtil()
        javaUtil.doSomething()
    }
}

static method

package com.example.ktobject;

public class JavaUtil {
    
    

    public static String anyBoy = "2023希望我们都更好,加油";

    public static void doSomething() {
    
    
        System.out.println("2023希望我们都更好,加油");
    }
}

static call

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
		//静态变量
		var anyBoy = JavaUtil.anyBoy
		//静态方法
        JavaUtil.doSomething()
    }
}

Kotlin static variables, constants, functions

关于静态变量、常量、函数(方法)在Kotlin的使用,主要有Object、companion object、顶层方法、 @JvmStatic注解、@JvmField注解等方式

静态变量、常量The contents of are placed in @JvmStatic、@JvmField注解the explanation

This is a Kt utility class I created, it's just a normal class

package com.example.ktobject

class KtUtil {
    
    

    fun doSomething() {
    
    
        println("2023希望我们都更好,加油")
    }
}

If called, you also need to call the method after creating an instance

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var ktUtil = KtUtil()
        ktUtil.doSomething()
    }
}

Singleton Object

Use Objectkeywords to decorate classes

补充、扩展

  • This method mainly Kt 单例uses the knowledge of
  • 不支持 Java 调用 Kt 方法
  • The class modified by this method can be regarded as a static class.内部方法均为静态方法(定制性弱了一点)
  • At the same Kttime you can also use open, data,internal
package com.example.ktobject

object KtUtil {
    
    

    fun doSomething() {
    
    
        println("2023希望我们都更好,加油")
    }
}

calling method

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        KtUtil.doSomething()
    }
}

icon

insert image description here

使用不用关键字修饰类时,As 给出的图示也不相同

  • class Modifies the class diagram
    insert image description here
  • Object Modified Class Diagram
    insert image description here

companion companion object

Here using companion objectthe wrapper method, only在该作用域内的方法方为静态方法

补充、扩展

  • It is more customizable, and you can define the calling methods of different methods
  • When calling companion objectdecoration , the principle is that a伴生类
  • 不支持 Java 调用 Kt 方法

warts

package com.example.ktobject

class KtUtil {
    
    

    fun doAnything() {
    
    
        println("普通方法,不支持类名.调用")
    }

    companion object {
    
    
        fun doSomething() {
    
    
            println("2023希望我们都更好,加油")
        }
    }

}

calling method

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //基础调用(先建实例,再调方法)
        KtUtil().doAnything()
        //静态调用
        KtUtil.doSomething()
    }
}

top-level class top-level method

顶层方法是指那些没有定义在任何类中的方法,Kotlin编译器会将所有顶层方法编译成静态方法

Supplement, expand

Create .Kt File triple trick

  • New - Kt File
    insert image description here
  • Naming-File
    insert image description here
  • initial content
    insert image description here

KtUtils

package com.example.ktobject

fun doSomething() {
    
    
    println("2023希望我们都更好,加油")
}

The call is slightly different, there is no need to pass the class name. The method can be called directly (this is the so-called top-level method)

package com.example.ktobject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        doSomething()
    }
}

icon

insert image description here


Annotate @JvmStatic, @JvmField (support Java tuning Kt)

我们使用 @JvmStatic 注解静态方法,使用@JvmField 注解静态参数(变量、常量)

  • The original objectmethod 不支持Java通过静态方式调用Kt静态方法does not need to be added to the method @JvmStaticto support the call
package com.example.ktobject

object KtUtil {
    
    

 	@JvmField
    var anyBoy:String ="2023希望我们都更好,加油"
    
    @JvmStatic
    fun doSomething() {
    
    
        println("2023希望我们都更好,加油")
    }
}
  • The original companion objectmethod 不支持Java通过静态方式调用Kt静态方法does not need to be added to the method @JvmStaticto support the call
package com.example.ktobject

class KtUtil {
    
    

    @JvmField
    var anyBoy:String ="2023希望我们都更好,加油"

    fun doAnything() {
    
    
        println("普通方法,不支持类名.调用")
    }


    companion object {
    
    
        @JvmStatic
        fun doSomething() {
    
    
            println("2023希望我们都更好,加油")
        }
    }

}

After adding corresponding comments @JvmField,@JvmStaticJava 可正常调 Kotlin 变量、方法等

insert image description here


static constant const

When using const, you must first understand the difference between var、valmodified variables

  • varmutable variable, can be changed
  • valRead-only variable, cannot be changed

注意: const 仅支持 val 只读变量!!!

const作用域Generally, there are three

  • 顶层方法
package com.example.ktobject

const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
  • object
package com.example.ktobject

object KtUtil {
    
    
    const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
}
  • companion object
package com.example.ktobject

class KtUtil {
    
    

    companion object{
    
    
        const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
    }
}

提示1:object、companion object 内修饰常量,可以不加const ,但是会报警告...

insert image description here

提示2:如果使用了const ,就不需要加 @JvmField 注解也支持Java调Kt咯

The main difference between Interest Extension → const valand valDecorate Object

引用性

  • No const is used, in kotlin, it is directly referenced
  • In java, the full path must be written and getterobtained through the method

Kt类

insert image description here

Java引用

insert image description here

可见性

  • const valVisibility is public final static, can be directly accessed
  • valVisibility is private final static, and valmethods are generated getNormalObject(), accessed through method calls

Guess you like

Origin blog.csdn.net/qq_20451879/article/details/128534213