[2023] Kotlin Tutorial Part 2 Object-Oriented and Functional Programming Chapter 14 Generics 14.1 Generic Functions 14.1.2 Multiple Type Parameters

[2023] Kotlin Tutorial

insert image description here

Part II Object Oriented and Functional Programming

Chapter 14 Generics

Use generics to maximize code reuse, protect type safety, and improve performance. The biggest impact of the generic feature on Kotlin is the use of generics in collections.

14.1 Generic functions

Generics can be applied to function declarations, property declarations, generic classes, and generic interfaces.

14.1.2 Multiple Type Parameters

Our previous generic function example used only one type parameter,

insert image description here

In fact, multiple type parameters can be declared at the same time, separated by commas ",", examples are as follows:

fun <T,U> addRectangle(a:T,b:U):Boolean {...}

Type parameters can not only declare the function parameter type, but also declare the return type of the function. The sample code is as follows:

fun <T,U> rectangleEquals(a:T,b:U):U {...}

Guess you like

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