简体   繁体   English

如何使用kotlin从另一个类调用同伴内部的扩展对象?

[英]How to call extension object inside companion from another class using kotlin?

I want to make playvideo using Exoplayer, but I have a little bit problem with how to access my extension function.我想使用 Exoplayer 制作播放视频,但我对如何访问我的扩展功能有一点问题。

import com.google.android.exoplayer2.ui.PlayerView

class playerViewadapter {
    companion object{
      fun PlayerView.loadView(){
      }
   }
}

but loadView extension from my AppCompatActivity not show但是我的AppCompatActivity loadView扩展没有显示

class Test:AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.test)
        playerViewadapter.loadView()
    }
}

How to call loadView from my AppCompatActivity如何从我的 AppCompatActivity 调用loadView

You have to be in context of the object in order to invoke something of it.您必须在对象的上下文中才能调用它的某些内容。

with(playerViewadapter) { } or playerViewadapter.apply { } should do it: with(playerViewadapter) { }playerViewadapter.apply { }应该这样做:

Example:例子:

with(playerViewadapter) {
    playerView.loadView()
}

Try it yourself自己试试

Why You define extension function in a companion object of another class?为什么要在另一个类的伴生对象中定义扩展函数? Isn't it better to define it in the file Where You have PlayerView or if it is not Your class, create a new file for utils functions?在您拥有PlayerView的文件中定义它,或者如果它不是您的类,为 utils 函数创建一个新文件不是更好吗?

class PlayerView
{
}

fun PlayerView.loadView()
{
}

And then You can call this function:然后你可以调用这个函数:

val playerView = PlayerView()
playerView.loadView()

暂无
暂无

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

相关问题 Kotlin-在另一个对象(不是类)内部可以替代伴侣对象吗? - Kotlin - any substitute for companion object inside of another object (not class)? 调用另一个 function 或伴随 object android Z4FBA3BC02B72EE9A687A1E5286E373 中的变量 C - Call another function or variable inside a companion object android kotlin from main thread Unity:调用 Android Kotlin Function Inside Companion Z497031794414A552435F90151C - Unity: Call Android Kotlin Function Inside Companion Object from Unity 使用伴侣对象返回Kotlin中的类的实例 - Using companion object to return an instance of the class in Kotlin 如何覆盖伴随对象内的 kotlin 接口 - How to override kotlin interface inside companion object 在使用它之前检查来自另一个类的伴随对象是否已初始化 - Check if an companion object from another class is initialized before using it how Unit test methods of Kotlin class with private constructor, companion object and extending another class without mocking - how Unit test methods of Kotlin class with private constructor, companion object and extending another class without mocking 如何在伴侣对象Kotlin中保留单例类对象的引用 - How to keep the reference of the singleton class object in the companion object, Kotlin 如何引用 java 活动 class 的公共方法来自 Kotlin 同伴 ZA8CFDE6331BD59EB2AC96F9666 - How to refer to a public method of a java Activity class from the Kotlin companion object? Kotlin Generics - 调用同伴 object 方法 - Kotlin Generics - Call companion object method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM