是否有可能在Kotlin中覆盖静态方法? [英] Is it possible to override static method in Kotlin?

查看:134
本文介绍了是否有可能在Kotlin中覆盖静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你想像我们有以下课程

Manager{
   public static void doSth(){
      // some logic
   };
}

如何在Kotlin中覆盖该方法?

How to override that method in kotlin?

我已经厌倦了使用

fun Manager.doSth(){}

但它适用于实例,而不是静态类型.

but Its applied to instance not a static type.

这样做的目的是避免使用PowerMockito

Purpose of doing that is to avoid using PowerMockito

推荐答案

简短回答

简短"说明

您只能覆盖虚拟方法,还可以在Java中隐藏/替换静态方法,但是您不能在Kotlin中隐藏静态"方法,因为静态方法已经不能使用子类限定符了.而且即使使用扩展方法,您也无法对静态或非静态方法进行操作,因为成员函数将始终获胜(请参见下面的示例).您可以做的是对父对象进行子类化,并添加一个新的伴随对象,该对象的方法与父对象的名称相同,然后从内部调用该父方法.

You can only override virtual methods and also you can shadow/replace static methods in Java, but you can't shadow a "static" method in Kotlin as the static method will not be available using the child class qualifier already. And even when using extension methods, you simply can't do it for either static or non-static as the member function will always win(see the example below). What you can do is to subclass the parent and add a new companion object that has a method with the same name as the parent and call the parent method from inside.

完整说明

  • 静态方法不能被覆盖.隐藏时称为阴影 一种方法与另一种方法.
  • Kotlin中没有静态方法,因此您可以使用 companion object的行为类似,您可以访问该方法 随播对象的效果,就好像它是仅使用java的静态方法一样 类名作为限定符,但是您无法访问 子类之类的父类的子对象,例如Java.
  • 您无法在Kotlin中隐藏静态方法,因为该静态方法已经无法使用子类限定符使用,但是您可以编写另一个伴随对象并添加具有相同名称的方法,然后调用 从那里的父方法,因为无法访问父方法 带有子类名称限定符的对象.
  • 您不能创建隐藏伴侣对象的扩展方法 方法甚至重写成员方法.如果班级有成员 函数和扩展函数具有相同的定义 接收者类型,具有相同的名称,并且适用于给定的参数, 成员总是赢.
  • 伴侣对象扩展:您可以编写扩展名方法 随播对象,但如果该随播对象的成员具有 成员将永远赢得相同的签名.
  • Static methods can't be overridden. It's called shadowing as you hide a method with another one.
  • There are no static methods in Kotlin, so what you can do is using the companion object which behaves similarly and you can access the method of the companion object as if it were a java static method using only the class name as a qualifier but you can't access the methods of companion object of a parent class from its child like Java.
  • You can't shadow a static method in Kotlin as the static method will not be available using the child class qualifier already, but you can write another companion object and add a method with the same name and call the parent method from there as the method from parent can't be accessed with the child class name qualifier.
  • You can't make an extension method that shadows a companion object method or even overrides a member method. If a class has a member function and an extension function is defined which has the same receiver type, the same name and is applicable to given arguments, the member always wins.
  • Companion object extension: You can write an extension method to the companion object, but if a member of that companion object has the same signature the member will always win.
open class A {
    fun foo() {
        println("A.foo()")
    }
    companion object {
        fun bar() {
            println("A.Companion.bar()")
        }
    }
}

class B: A()

fun A.foo() {
   println("A.foo() extension")
}

fun A.Companion.bar() {
    println("A.Companion.bar() extension")
}

fun A.Companion.baz() {
    println("A.Companion.baz() extension")
}

fun main(args: Array<String>) {
    A().foo() // prints A.foo()
    A.bar() // prints A.Companion.bar()
    A.baz() // prints A.Companion.baz() extension
    B.bar() // COMPILE ERROR
}

这篇关于是否有可能在Kotlin中覆盖静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆