首页 > 解决方案 > 如何通过一个方法更新整个 kotlin 类中的 var,然后用另一个方法调用更新它

问题描述

我编写了一个 kotlin 类,它具有被覆盖的乐趣和将 var 更新到类范围的乐趣(可悲的是,我是 Kotlin 的新手!)

class mySampleClass: sampleReference(){
    var varToBeUpdated:String = "my string" //var in class scope to be updated

    fun updateMyVar(gotString:String){

        //I tried this, it didn't work
        this.varToBeUpdated = gotString
        // also this didn't work
        varToBeUpdated = gotString

    }

    override fun sample(context: Context, intent: Intent){
        //here i need my varToBeUpdated with new string
        runSomeThing(varToBeUpdated)
        //some work to be done here
    }
}

在我调用方法的地方:

myObject.updateMyVar("new string")
myObject.sample()

我想知道如何更新我需要的 var,因为我无法在“有趣的样本”中添加新的 args,因为它覆盖了一个类方法

在此先感谢,向大家致以最诚挚的问候:)



更新:添加我的实际代码,因为当我调用覆盖方法时,该类似乎无法保持正确的更新值:

这是我的 BroadcastReceiver,检查下载是否完成并执行一些操作

class DownloadBroadcastManager: BroadcastReceiver() {

    var myClassFilename:String = "default"
    var myClassExtension:String = ".default"

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action

        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
            //Show a notification
            // here there's a log to check if var are updated
            println("myTag - variables $myClassFilename, $myClassExtension")

            Toast.makeText(context, "Download of $myClassFilename$myClassExtension completed", Toast.LENGTH_LONG).show()
            // richiama azioni come player o display image o altro?

            //player
            var uri = Uri.parse (Environment.getExternalStorageDirectory().getPath() + "/Download/$myClassFilename$myClassExtension") //myClassExtension is ".mp3", dot is included, however it seems class is re-intialized as i call the method
            println("myTag - uri: $uri")
            println("myTag - context: $context")

            var mPlayer = MediaPlayer() // I added this declaration (that's be re-done later) cause I had a problem in making the player running (of course giving it a valid path to a valid file). Now this is "junk code"
            mPlayer.stop()
            mPlayer.reset()
            mPlayer.release()
            mPlayer = MediaPlayer.create(context, uri) // here there's the proper declaration + initialization
            mPlayer.start()

        }
    }
}

这是我的 DownloaderClass 的一部分...

var brReceiver = DownloadBroadcastManager()
    // shows when download is completed
    println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: originals") //here shows the default: it's right
    val intent = Intent(context, MainActivity::class.java)
    brReceiver.myClassFilename = myTitle // inject filename
    brReceiver.myClassExtension = ".mp3" // inject file extension
    println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: modified") // here it shows my class property as correctly updated

    brReceiver.onReceive(context, intent) // here, as calling the override fun, it get back to default value of the property

标签: androidkotlinbroadcastreceiverandroid-broadcastreceiver

解决方案


您可以执行以下操作:

  1. 摆脱updateMyVar功能:

    class MySampleClass: SampleReference(){
        var varToBeUpdated:String = "my string" //var in class scope to be updated
    
        override fun sample(context: Context, intent: Intent){
            //here i need my varToBeUpdated with new string
            runSomeThing(varToBeUpdated)
            //some work to be done here
        }
    }
    
  2. 直接更新varToBeUpdated属性:

    val myObject = MySampleClass()
    myObject.varToBeUpdated = "new string"
    myObject.sample()
    

更新:如果您调用brReceiver.onReceive(...)其中的值,DownloadBroadcastManager则会更新。但你不应该那样做。Android 框架会为您调用它。并且当它发生时,会创建新的DownloadBroadcastManager类实例并设置默认值。我们使用Intent将数据传递给BroadcastReceiver,例如intent.putExtra("filename", "yourFileName")在创建时BroadcastReceiver调用,并intent.getStringExtra("filename")onReceive()函数中调用以获取值。以下是如何传递/获取数据到/从BroadcastReceiver


推荐阅读