[2023] Kotlin Tutorial Part 2 Object-Oriented and Functional Programming Chapter 13 Functional Programming Cornerstone - Higher-order Functions and Lambda Expressions 13.5 Inline Functions 13.5.3 Using with and apply functions

[2023] Kotlin Tutorial

insert image description here

Part II Object Oriented and Functional Programming

Chapter 13 The Cornerstone of Functional Programming—Higher-order Functions and Lambda Expressions

Although the idea of ​​functional programming is as old as object-oriented, the computer language that supports functional programming is only a matter of recent years. These languages ​​include Swift, Python, Java 8, and C++ 11. As a new language, Kotlin also supports functional programming.

13.5 Inline functions

In a high-order function, if the parameter is a function type, it can receive a Lambda expression, and the Lambda expression is compiled into an anonymous class at compile time, and an object will be created every time the function is called. If this is called repeatedly by the function, then Creating many objects will incur runtime overhead.

To solve this problem, such functions can be declared as inline functions in Kotlin.

[Tips] The inline function will not generate function call code when compiling, but replace each function call with the actual code in the function body.

13.5.3 Using the with and apply functions

Sometimes you need to set multiple properties on an object, or call multiple functions, you can use the with or applyl function. Similar to the let function, all objects in Kotlin can use these two functions.

Take a chestnut:

import java.awt.BorderLayout
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel

class MyFrame(title: String) : JFrame(title) {
    
    

    init {
    
    
        // 创建标签
        val label = JLabel("Label")

        // 创建Button1
        val button1 = JButton()
        button1.text = "Button1"
        button1.toolTipText = "Button1"

        // 注册事件监听器, 监听Button1 的单击事件
        button1.addActionListener {
    
     label.text = "单击Button1" }

        // 创建Button2
        val button2 = JButton().apply {
    
    
            text = "Button2"
            toolTipText = "Button2"

            // 注册事件监听器, 监听Button2 的单击事件
            addActionListener {
    
     label.text = "单击Button2" }

            // 添加Button2 到内容面板
            contentPane.add(this, BorderLayout.SOUTH)
        }

        with(contentPane) {
    
    
            // 添加标签到内容面板
            add(label, BorderLayout.NORTH)

            // 添加Button1 到内容面板
            add(button1, BorderLayout.CENTER)

            println(height)
            println(this.width)
        }

        // 设置窗口大小
        setSize(350, 120)

        // 设置窗口可见
        isVisible = true
    }
}

fun main() {
    
    

    // 创建Frame 对象
    MyFrame("MyFrame")
}

insert image description here

The above code is a Swing window, and Swing is an introduction to Java's graphical user interface [we will talk about it later]

The code creates and calls the properties and functions of Button1. In this traditional approach, since the properties or functions of the same object are called multiple times, you can use the with or apply function

Create and call the properties and functions of Button2 later, using the applyt function. The apply function is followed by a Lambda expression. The properties and functions that need to be called are placed in the Lambda expression. The object name button2 omitted in the Lambda expression, for example, the text="Button2expression The formula shows the text attribute of the button2 to be called. If you refer to the current object in the apply function, you can use the this keyword. For example, contentPane.add(this, BorderLayout..SOUTH)in this, the apply function has a return value, and its return value is the current object.

If you don’t need to return a value, you can use the with function. The with function is similar to the apply function, followed by a Lambda expression. The properties and functions that need to be called are placed in the Lambda expression. If the current object is referenced in the with function, the this key is also used. Character.

Guess you like

Origin blog.csdn.net/weixin_44226181/article/details/130024513