Kotlin 中是否存在构造函数引用?
大家好!我最近开始学习 Kotlin,遇到了这样一个情况。
我知道 Kotlin 中有函数引用(function references)——例如 `String::length` 或 `::println`。当需要将函数作为参数传递给另一个函数时,这非常方便。
但是构造函数呢?我有一个简单的类:
```kotlin
data class Person(val name: String, val age: Int)
```
我想将这个类的新实例创建作为参数传递给高阶函数。例如,有一个函数:
```kotlin
fun createEntity(factory: (String, Int) -> T): T {
return factory("Иван", 25)
}
```
我能否像在 Java 中那样写 `createEntity(Person)` 或 `createEntity(Person::new)`?或者 Kotlin 中是否有其他用于构造函数引用的语法?
我尝试过写 `createEntity(Person::Person)`,但编译器报错。请告诉我如何正确实现。提前感谢!
<<>>