Kotlin 简明教程

Kotlin - Generics

与 Java 一样,Kotlin 提供了称为泛型的变量类型的更高阶。在本章中,我们将学习 Kotlin 如何实现泛型,以及作为开发人员,我们如何使用泛型库中提供的那些功能。在实现方面,泛型与 Java 非常相似,但 Kotlin 开发人员引入了两个新关键字 “out”“in” ,以使 Kotlin 代码更具可读性和更容易被开发者理解。

Like Java, Kotlin provides higher order of variable typing called as Generics. In this chapter, we will learn how Kotlin implements Generics and how as a developer we can use those functionalities provided inside the generics library. Implementation wise, generics is pretty similar to Java but Kotlin developer has introduced two new keywords “out” and “in” to make Kotlin codes more readable and easy for the developer.

在 Kotlin 中,类和类型是完全不同的概念。根据示例,List 在 Kotlin 中是一个类,而 List<String> 在 Kotlin 中是一个类型。下面的示例描述了泛型如何在 Kotlin 中实现。

In Kotlin, a class and a type are totally different concepts. As per the example, List is a class in Kotlin, whereas List<String> is a type in Kotlin. The following example depicts how generics is implemented in Kotlin.

fun main(args: Array<String>) {
   val integer: Int = 1
   val number: Number = integer
   print(number)
}

在上面的代码中,我们声明了一个“整数”,然后将该变量赋值给一个数字变量。这是可能的,因为“Int”是 Number 类的子类,因此类型转换在运行时自动发生,并产生“1”的输出。

In the above code, we have declared one “integer” and later we have assigned that variable to a number variable. This is possible because “Int” is a subclass of Number class, hence the type conversion happens automatically at runtime and produces the output as “1”.

让我们进一步了解 Kotlin 中的泛型。最好在不确定我们在应用程序中要使用的数据类型时使用泛型数据类型。通常,Kotlin 中的泛型由 <T> 定义,其中“T”代表模板,它可以由 Kotlin 编译器动态确定。在以下示例中,我们将看到如何在 Kotlin 编程语言中使用泛型数据类型。

Let us learn something more about generics in Kotlin. It is better to go for generic data type whenever we are not sure about the data type we are going to use in the application. Generally, in Kotlin generics is defined by <T> where “T” stands for template, which can be determined dynamically by Kotlin complier. In the following example, we will see how to use generic data types in Kotlin programming language.

fun main(args: Array<String>) {
   var objet = genericsExample<String>("JAVA")
   var objet1 = genericsExample<Int>(10)
}
class genericsExample<T>(input:T) {
   init {
      println("I am getting called with the value "+input)
   }
}

在上面的代码段中,我们创建了一个具有泛型返回类型的类,表示为 <T> 。看看 main 方法,我们在运行时动态定义它的值,同时创建该类的对象时提供值类型。这就是 Kotlin 编译器如何解释泛型的。一旦我们在编码基础中运行此代码,我们将在浏览器中获得以下输出。

In the above piece of code, we are creating one class with generic return type, which is represented as <T>. Take a look at the main method, where we have dynamically defined its value at the run by proving the value type, while creating the object of this class. This is how generics is interpreted by Kotlin compiler. We will get the following output in the browser, once we run this code in our coding ground.

I am getting called with the value JAVA
I am getting called with the value 10

当我们要将泛型类型分配给它的任何超类型时,我们需要使用“out”关键字,当我们要将泛型类型分配给它的任何子类型时,我们需要使用“in”关键字。在以下示例中,我们将使用“out”关键字。同样,你也可以尝试使用“in”关键字。

When we want to assign the generic type to any of its super type, then we need to use “out” keyword, and when we want to assign the generic type to any of its sub-type, then we need to use “in” keyword. In the following example, we will use “out” keyword. Similarly, you can try using “in” keyword.

fun main(args: Array<String>) {
   var objet1 = genericsExample<Int>(10)
   var object2 = genericsExample<Double>(10.00)
   println(objet1)
   println(object2)
}
class genericsExample<out T>(input:T) {
   init {
      println("I am getting called with the value "+input)
   }
}

上面的代码将在浏览器中产生以下输出。

The above code will yield the following output in the browser.

I am getting called with the value 10
I am getting called with the value 10.0
genericsExample@28d93b30
genericsExample@1b6d3586