简体   繁体   English

在 Kotlin 中访问同伴对象中的父类变量

[英]Access Parent class variables in companion Object in Kotlin

I am trying to call static function of one class in other like java , But in kotlin I can not make a static function , and I have to make a companion object in which I have to define my function , But while doing this I am not able to access parent class variables , is there any way I can achieve this in kotlin .我试图在其他类中调用一个类的静态函数,例如 java,但是在 kotlin 中我无法创建静态函数,我必须创建一个必须在其中定义我的函数的伴侣对象,但是在执行此操作时我不是能够访问父类变量,有什么办法可以在 kotlin 中实现这一点。

class One {

    val abcList = ArrayList<String>()

    companion object {

        fun returnString() {
            println(abcList[0]) // not able to access abcList here
        }
    }
}

class Two {

    fun tryPrint() {
        One.returnString()
    }
}
// In Java we can do it like this

class One {

    private static ArrayList<String> abcList = new ArrayList<>();

    public void tryPrint() {
        // assume list is not empty 
        for(String ab : abcList) {
            System.out.println(ab);
        }
    }

    public static void printOnDemand() {
        System.out.println(abcList.get(0));
    }
}

class Two {

    public void tryPrint(){
        One.printOnDemand();
    }
}

I want to access fun returnString() like static function of class one like we do in java , if any one have achieved this please help .我想像我们在 java 中所做的那样,像第一类的静态函数一样访问有趣的 returnString(),如果有人已经实现了这一点,请帮忙。

In your case abcList is a member variable of the class.在您的情况下, abcList是该类的成员变量。 Each instance of a class has their own version of its member variables which means that a static method cannot access them.类的每个实例都有自己的成员变量版本,这意味着静态方法无法访问它们。 If you want to access it from your companion object it has to be static too.如果你想从你的伴生对象访问它,它也必须是静态的。

class One {
    companion object {
        val abcList = ArrayList<String>()

        fun returnString() {
            println(abcList[0])
        }
    }
}

class Two {
    fun tryPrint() {
        One.returnString()
    }
}

This code will work, but keep in mind that in this case there will be only one instance of abcList .这段代码可以工作,但请记住,在这种情况下,将只有一个abcList实例。 Accessing a member variable from a static function is not possible (not even in Java).从静态函数访问成员变量是不可能的(甚至在 Java 中也不可能)。

Here's the Kotlin version of your Java example:这是您的 Java 示例的 Kotlin 版本:

class One {
    companion object {
        val abcList = ArrayList<String>()

        fun printOnDemand() {
            println(abcList[0])
        }
    }

    fun tryPrint() {
        for (ab in abcList) {
            println(ab)
        }
    }
}

class Two {
    fun tryPrint() {
        One.printOnDemand()
    }
}

Rule: You can't access static properties, members of a class in none static members and you can't access none static properties, members of a class in static members which is the companion object class.规则:您不能访问静态属性,非静态成员中的类成员,并且您不能访问非静态属性,静态成员中的类成员,即伴随对象类。 This rule is in both Java and Kotlin.这个规则在 Java 和 Kotlin 中都有。 If you want to access a none static member of a class inside static members you have to declare it inside companion object class.如果要访问静态成员中类的非静态成员,则必须在伴随对象类中声明它。

Use the following code for you case.对您的情况使用以下代码。

object One {
    val abcList: MutableList<String> = mutableListOf()

    fun returnString() {
       println(abcList[0])
    }

    fun printOnDemand() {
       println(abcList[0]);
    }
}

class Two {
    fun tryPrint() {
        One.printOnDemand()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM