kotlin中的全局扩展函数

嘿,我想在kotlin上做一个类,它将包含我将在几个地方使用的所有扩展函数,例如:

class DateUtils { //in this case I use jodatime fun Long.toDateTime() : DateTime = DateTime(this) fun String.toDateTime() : DateTime = DateTime.parse(this) } class SomeClassWithNoConnectionToDateUtils { fun handleDataFromServer(startDate: String) { someOtherFunction() //startDate knows about toDateTime function in DateUtils startDate.toDateTime().plusDays(4) } } 

有没有办法来执行这样的操作

DateUtils类中使用扩展将使它们仅在DateUtils类中使用。

如果你希望这些扩展是全局的,你可以把它们放在一个文件的顶层,而不用把它们放到一个类中。

 package com.something.extensions fun Long.toDateTime() : DateTime = DateTime(this) fun String.toDateTime() : DateTime = DateTime.parse(this) 

然后导入它们在其他地方使用,如下所示:

 import com.something.extensions.toDateTime val x = 123456L.toDateTime()