<i id='VYtFO'><tr id='VYtFO'><dt id='VYtFO'><q id='VYtFO'><span id='VYtFO'><b id='VYtFO'><form id='VYtFO'><ins id='VYtFO'></ins><ul id='VYtFO'></ul><sub id='VYtFO'></sub></form><legend id='VYtFO'></legend><bdo id='VYtFO'><pre id='VYtFO'><center id='VYtFO'></center></pre></bdo></b><th id='VYtFO'></th></span></q></dt></tr></i><div id='VYtFO'><tfoot id='VYtFO'></tfoot><dl id='VYtFO'><fieldset id='VYtFO'></fieldset></dl></div>
<legend id='VYtFO'><style id='VYtFO'><dir id='VYtFO'><q id='VYtFO'></q></dir></style></legend>

<tfoot id='VYtFO'></tfoot>
    • <bdo id='VYtFO'></bdo><ul id='VYtFO'></ul>
  • <small id='VYtFO'></small><noframes id='VYtFO'>

        如何将实现委托给 Kotlin 中的可变属性?

        时间:2024-08-25

        <i id='QhEfw'><tr id='QhEfw'><dt id='QhEfw'><q id='QhEfw'><span id='QhEfw'><b id='QhEfw'><form id='QhEfw'><ins id='QhEfw'></ins><ul id='QhEfw'></ul><sub id='QhEfw'></sub></form><legend id='QhEfw'></legend><bdo id='QhEfw'><pre id='QhEfw'><center id='QhEfw'></center></pre></bdo></b><th id='QhEfw'></th></span></q></dt></tr></i><div id='QhEfw'><tfoot id='QhEfw'></tfoot><dl id='QhEfw'><fieldset id='QhEfw'></fieldset></dl></div>
          <tbody id='QhEfw'></tbody>

        <small id='QhEfw'></small><noframes id='QhEfw'>

                <bdo id='QhEfw'></bdo><ul id='QhEfw'></ul>
              • <tfoot id='QhEfw'></tfoot>
                <legend id='QhEfw'><style id='QhEfw'><dir id='QhEfw'><q id='QhEfw'></q></dir></style></legend>

                • 本文介绍了如何将实现委托给 Kotlin 中的可变属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  据我了解,在 Kotlin 中委派实现的想法是避免出现如下代码:

                  As to my understanding, the idea of delegating an implementation in Kotlin is to avoid code that looks like this:

                  class MyClass(val delegate : MyInterface) : MyInterface
                  {
                      override fun myAbstractFun1() = delegate.myAbstractFun1()
                      override fun myAbstractFun2() = delegate.myAbstractFun2()
                      // ...
                  }
                  

                  相反,我们可以编写以下代码来做同样的事情:

                  Instead, we can write the following code which should do the same:

                  class MyClass(val delegate : MyInterface) : MyInterface by delegate
                  

                  现在,我希望 delegate 成为可变变量,即我的代码如下所示:

                  Now, I'd like delegate to be a mutable variable, i.e. my code looks like this:

                  var delegate : MyInterface = MyImplementation()
                  object MyObject : MyInterface by delegate
                  

                  因此,如果我像第一个示例一样将每个抽象方法委托给 delegate,那么更改 delegate 的值确实会改变方法的行为.但是,上面的代码编译成这个 Java 代码:

                  So if I'd delegated every abstract method to delegate by myself like in the first example, changing the value of delegate does change the behaviour of the methods. However, the above code compiles to this Java code:

                  public final class MyObject implements MyInterface {
                      public static final MyObject INSTANCE;
                      // $FF: synthetic field
                      private final MyInterface $$delegate_0 = MyObjectKt.access$getDelegate$p();
                  
                      @NotNull
                      public String myAbstractFun1() {
                          return this.$$delegate_0.myAbstractFun1();
                      }
                  
                      @NotNull
                      public String myAbstractFun2() {
                          return this.$$delegate_0.myAbstractFun2();
                      }
                  }
                  

                  很明显,Kotlin 编译器决定在创建 MyObject 时将其复制到最终字段 $$delegate_0delegate 字段code>,当我改变 delegate

                  So obviously, instead of just using the delegate field, the Kotlin compiler decides to copy it when creating MyObject to a final field $$delegate_0, which is not modified when I change the value of delegate

                  有没有更好的解决方案来代替手动委派每个方法?

                  Is there a better solution for doing this instead of delegating every method manually?

                  推荐答案

                  可悲的是,据我所知,没有办法通过更改原始属性内容来更改委托,您可能仍然能够通过以不可变的方式工作并复制对象来做类似的事情:

                  Sadly, as far as I know there is no way of changing the delegate by changing the original property content, but you might still be able to do something similar by working in an immutable way and copying the object:

                  interface MyInterface {
                    fun foo():Int
                  }
                  
                  data class MyClass(val delegate : MyInterface) : MyInterface by delegate
                  
                  object ImplementationA: MyInterface { override fun foo() = 7 }
                  object ImplementationB: MyInterface { override fun foo() = 5 }
                  
                  val objA = MyClass(ImplementationA)
                  println(objA.foo()) // returns 7
                  
                  val objB = objA.copy(ImplementationB)
                  println(objB.foo()) // returns 5
                  println(objA.foo()) // still 7
                  

                  希望这仍然有用.

                  这篇关于如何将实现委托给 Kotlin 中的可变属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Objective-C 等价于 Java 在类方法中的匿名类 下一篇:将 Java Swing 桌面应用程序国际化的最佳实践是什么?

                  相关文章

                • <small id='AGHp9'></small><noframes id='AGHp9'>

                    1. <legend id='AGHp9'><style id='AGHp9'><dir id='AGHp9'><q id='AGHp9'></q></dir></style></legend>
                      <i id='AGHp9'><tr id='AGHp9'><dt id='AGHp9'><q id='AGHp9'><span id='AGHp9'><b id='AGHp9'><form id='AGHp9'><ins id='AGHp9'></ins><ul id='AGHp9'></ul><sub id='AGHp9'></sub></form><legend id='AGHp9'></legend><bdo id='AGHp9'><pre id='AGHp9'><center id='AGHp9'></center></pre></bdo></b><th id='AGHp9'></th></span></q></dt></tr></i><div id='AGHp9'><tfoot id='AGHp9'></tfoot><dl id='AGHp9'><fieldset id='AGHp9'></fieldset></dl></div>
                      <tfoot id='AGHp9'></tfoot>
                        <bdo id='AGHp9'></bdo><ul id='AGHp9'></ul>