Getting Started with Kotlin Basics - Variables, Functions (Methods), Objects, Inheritance, Interfaces

Although I used Kotlin for a while before, but looking back, I didn’t make notes in this area, so I simply recorded it systematically.

base over

自语:Kotlin语法糖真是秀儿,话说,并不是Java不如Kotlin,而是Google在推属于android自己的语言

This article mainly records some Kotlinbasic usage methods for your convenience. 快速从Java转战到KotlinFor simplicity and clarity, I will 综合Java、Kt的不同使用record them together. If you are interested, you can also go to Google's Kotlin documentation to learn~

How to declare variables?

Java

 String data = "宋老三";

Kotlin

在Kotlin声明变量时,通常有俩种方式

  • varassignment can be modified
  • valOnly supports initialization assignment, refer to Java finalkeywords

Kotlin Regular

var dataVar: String = "宋老三"
val dataVal: String = "宋老三"

Simplified version of Kotlin ( Kt 自带类型推断features, variable types can be inferred based on assigned data)

var dataVar = "宋老三"
val dataVal = "宋老三"

How to declare methods and functions?

函数又称为方法,是具有特定功能的一段独立程序。

We are declaring Java中经常将函数说成方法,在Kotlin中称为函数更多一些the main use of the function , the following is generallyfun函数函数组成结构

函数声明  函数名称([参数名称:参数类型,参数名称:参数类型]):返回值类型{ 
    执行语句 
    return 返回值 
}
  • Function Declaration: Function declarations in Kotlin use keywords fun.
  • Function name: 每一个函数都有函数名称, convenient to use when calling the function.
  • Parameter type: used to limit when calling a function 传入参数的数据类型.
  • Parameter name: is a 变量, used for 接收调用函数时传入的数据.
  • Return value type: for 限定函数返回值的数据类型.
  • Return value: was return语句返回的值,该值会返回给调用者.

no parameter no return value

Java

 public void test() {
    
    } 

Kotlin

 fun test() {
    
    
    println("这是一个无参无返回值的函数") 
 }

返回 Unit类型,可不写

 fun test(): Unit {
    
     
    println("这是一个无参无返回值的函数") 
 }

Return value without parameter

Kotlin

 fun test(): String {
    
     
    return " 这是一个无参有返回值的函数" 
 }

With parameters but without return value

Java

 public void test(int num1) {
    
    } 

Kotlin

 fun test(num1: Int) {
    
    
     println("这是一个无参无返回值的函数") 
 }

have parameters and return value

Java

 public int test(int num1, int num2) {
    
    
    return num1 + num2;
 }

Kotlin

注意:档方法需要return返回值时,在方法后声明返回类型

 fun test(num1: Int, num2: Int): Int {
    
    
    return num1 + num2
 }

single expression function

Syntactic sugar - can be used directly when the return value is only one line= 返回

fun test(num1: Int, num2: Int): Int = num1 + num2

How to create objects?

Java

Create a class (including set method, get method, construction method)

public class Dream {
    
    
    public String what;
    public String time;

    public Dream() {
    
    
        super();
    }
 
    public Dream(String what, String time) {
    
    
        this.what = what;
        this.time = time;
    }

    public String getWhat() {
    
    
        return what;
    }

    public void setWhat(String what) {
    
    
        this.what = what;
    }

    public String getTime() {
    
    
        return time;
    }

    public void setTime(String time) {
    
    
        this.time = time;
    }
}

create object

Dream info = new Dream("Life","2022");

Kotlin

create class( 包含set方法、get方法、有参构造方法)

open class Dream {
    
    
    //父类无参的次构造器
    constructor() {
    
    
        println("无参构造器")
    }

    //父类无参的次构造器
    constructor(what: String, time: String) {
    
    
        println("无参构造器")
    }
}

Simplified writing, but there is no no-argument construction

open class Dream(var what: String, var time: String) {
    
    }

create object

var dream: Dream = Dream("Life", "2022")
//类型推导
var dream = Dream("Life","2022")

How class inheritance?

Java

public class Son extends Dream{
    
    }

Kotlin

class Son : Dream(){
    
    }

How to implement the interface?

Java

create interface

public interface DreamListener {
    
    
    public void gogo();
}

interface implementation

实现方式 → implements +接口

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements DreamListener {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void gogo() {
    
    
        
    }
}

Kotlin

create interface

interface DreamListener {
    
    
    fun gogo()
}

interface implementation

实现方式 → :+接口

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

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

    override fun gogo() {
    
    

    }
}

Guess you like

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