如何在 System 类上创建一个扩展函数?

huangapple go评论56阅读模式
英文:

How can I create an extension function on System class?

问题

我正在尝试在 java.lang.System 上创建一个扩展函数,以将 currentTimeMillis 的使用重构为以下返回 StringSystem.currentTimeinSeconds() 扩展方法。(这是为了避免重复代码并在代码中重用它)

这是我尝试过的代码,但它不起作用:

fun System.currentTimeInSeconds () : String {
    return ((this.currentTimeMillis / 1000).toString()) 

有关为什么不起作用的任何指导?谢谢。

英文:

I am trying to create an extension function on java.lang.System to refactor currentTimeMillis usage to the following System.currentTimeinSeconds() extension method that returns a String. (This is done to avoid duplicate code and to reuse it in the code)

This is what I have tried but its not working:

fun System.currentTimeInSeconds () : String {
    return ((this.currentTimeMillis / 1000).toString()) 

Any guidance as to why this is not working? Thanks.

答案1

得分: 2

你不能这样做。Kotlin目前只支持在对象上使用扩展函数,而不是类,而System是一个类。

英文:

You cannot do this. Kotlin currently only supports extension functions on objects, not classes, and System is a class.

答案2

得分: 0

欢迎来到 Stack Overflow,首先,重申类和对象之间的区别很重要:

  • 一个类是用于创建对象的蓝图或模板。它定义了对象的属性和行为。
  • 另一方面,对象是类的一个实例。它是使用类提供的蓝图或模板创建的。

现在,让我们看看 Kotlin 扩展函数的相关信息:

文档指出:

您可以为无法修改的第三方库中的类或接口编写新函数。
这些函数可以像原始类的方法一样以通常的方式调用。这种机制称为扩展函数。还有扩展属性,允许您为现有类定义新属性。

而且,如果您进一步查看:

扩展函数中的 this 关键字对应于 接收对象(即在点之前传递的对象)。现在,您可以在任何 MutableList<Int> 上调用这样的函数:

因此,扩展函数作用于接收的对象,而不是蓝图(类)本身。

System 是一个类,currentTimeMillis() 是一个静态本机方法。因此,当您尝试为其创建扩展函数时,您的扩展函数将在类本身上调用,而不是在类的对象上调用,因为您一直从 System 中调用静态方法。

接下来,您的需求真的需要这样的扩展函数吗?您在调用静态方法,创建一个函数应该足够了。(即,您已经拥有所需的所有信息)

fun stringifedTimeInSeconds() =
    System.currentTimeMillis().div(1000).toString()

最后,也许值得阅读以下问题中的一些答案:

希望这对您有所帮助。

英文:

Welcome to SO, first, it's important to reiterate the difference between a class and an object:

  • A class is a blueprint or a template for creating objects. It defines the properties and behaviour of objects.
  • On the other hand, an object is an instance of a class. It is created using the blueprint or template provided by the class.

Now that's out of the way, you can find decent amount of information of Kotlin Extension functions here:

The documentation states:

> you can write new functions for a class or an interface from a third-party library that you can't modify.
> Such functions can be called in the usual way, as if they were methods of the original class. This
> mechanism is called an extension function. There are also extension properties that let you define new
> properties for existing classes.

And if you further looked down:

> The this keyword inside an extension function corresponds to the receiver object (the one that is passed
> before the dot). Now, you can call such a function on any MutableList<Int>:

So the extension function acts on a received object, not the blueprint (class) itself.

System is a class and the currentTimeMillis() is a static native method. Therefore, when you try to
create an extension function for it, your extension function will be called on the class itself, not on an
object of the class in your case as you were calling the static method all along from System.

Next, does your requirements really need an extension function for this? You're calling a static, creating a function should be enough. (i.e. You have all the information you need)

fun stringifedTimeInSeconds() =
    System.currentTimeMillis().div(1000).toString()

Finally, it might be worth reading some answers in the following question:

Hope this helps.

答案3

得分: 0

以下是已翻译的内容:

顺便提一下,官方 Kotlin 演进和增强过程(KEEP)中有一个关于此的提案。
您可以在 Kotlin 静态和静态扩展 中查看详细信息。
但不幸的是,由于提案已从当前路线图中删除,我们将在不久的将来看不到任何进展。

如何在 System 类上创建一个扩展函数?
https://kotlinlang.org/docs/roadmap.html#new-items

英文:

By the way, there is a proposal in the official Kotlin Evolution and Enhancement Process (KEEP) for this.
You can check out the details in Kotlin statics and static extensions.
But unfortunately we won't see any progress in the near feature, because the proposal was removed from the current roadmap.

如何在 System 类上创建一个扩展函数?
https://kotlinlang.org/docs/roadmap.html#new-items

huangapple
  • 本文由 发表于 2023年4月1日 00:23:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定