kotlinKotlin学习kotlin

Kotlin学习 12 -- Java 与 Kotlin 代码的

2020-08-05  本文已影响0人  开心wonderful

本篇文章主要介绍以下几个知识点:

SUMMER DAY (图片来源于网络)

1. 把 Java 代码转换成 Kotlin 代码

把 Java 代码转换成 Kotlin 代码,在语法层面是有一定规律的,而 Android Studio 提供了两种一键完成转换的功能,比如有如下 Java 代码:

    public void printFruits() {
        List<String> fruitList = new ArrayList<>();
        fruitList.add("apple");
        fruitList.add("grape");
        fruitList.add("orange");
        for (String fruit : fruitList) {
            System.out.println(fruit);
        }
    }
把 Java 代码转换成 Kotlin 代码的提示框 将 Java 文件转换成 Kotlin 文件

转换后的 Kotlin 代码如下:

fun printFruits(): Unit {
    val fruitList: MutableList<String> = ArrayList()
    fruitList.add("apple")
    fruitList.add("grape")
    fruitList.add("orange")
    for (fruit in fruitList) {
        println(fruit)
    }
}

这样转换很方便,但它只会按照固定的语法编号规律来执行转换工作,而不会自动运用 Kotlin 的优秀特性,从而只能实现基础版的 Kotlin 语法,有很大的优化空间。比如上面的代码用如下实现会更好:

fun printFruits() {
    val fruitList = mutableListOf("apple", "grape", "orange")
    for (fruit in fruitList) {
        println(fruit)
    }
}

2. 把 Kotlin 代码转换成 Java 代码

很遗憾,Android Studio 中并没有提供把 Kotlin 代码转换成 Java 代码的功能,因为 Kotlin 拥有很多 Java 中并不存在的特性,很难执行一键转换。

不过,可以先将 Kotlin 代码转换成 Kotlin 字节码,然后通过反编译的方式将它还原成 Java 代码

反编译出的代码不一定能直接正常运行,却能帮助理解许多 Kotlin 特性背后的实现原理。

举个栗子,kotlin-android-extensions 插件可以简化 Activity 中的代码编写,无需通过 findViewById() 来获取控件实例:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 在 activity_main.xml 布局中定义了 id 值为 button 的按钮,在这可直接调用
        button.setOnClickListener { "text".showToast(this) }  
    }
}

为探究 kotlin-android-extensions 插件实现上面功能的原理,可先将这段代码转换成 Kotlin 字节码,点击 AS 导航栏中的 Tools -> Kotlin -> Show Kotlin Bytecode,显示如下:

显示 Kotlin 字节码的窗口

接着点击这个窗口左上角的 Decompile 按钮,把字节码反编译成 Java 代码如下:

public final class MainActivity extends AppCompatActivity {
   private HashMap _$_findViewCache;

   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setContentView(1300051);
      ((Button)this._$_findCachedViewById(id.button)).setOnClickListener((OnClickListener)(new OnClickListener() {
         public final void onClick(View it) {
            ToastKt.showToast$default("text", (Context)MainActivity.this, 0, 2, (Object)null);
         }
      }));
   }

   public View _$_findCachedViewById(int var1) {
      if (this._$_findViewCache == null) {
         this._$_findViewCache = new HashMap();
      }

      View var2 = (View)this._$_findViewCache.get(var1);
      if (var2 == null) {
         var2 = this.findViewById(var1);
         this._$_findViewCache.put(var1, var2);
      }

      return var2;
   }

   public void _$_clearFindViewByIdCache() {
      if (this._$_findViewCache != null) {
         this._$_findViewCache.clear();
      }
   }
}

通过这段代码,大概可以了解 kotlin-android-extensions 插件背后的实现原理:

它会在 Activity 中自动生成一个 _$_findCachedViewById 方法(取这么奇怪的名字是为了防止和自定义的方法重名),在这方法中根据传入的 id 值调用 findViewById() 来查询并获取实例,然后用 HashMap 对该实例进行缓存,接下来在 onCreate 中只需调用 _$_findCachedViewById 获得 button 按钮的实例。

本篇文字就介绍到这。

上一篇 下一篇

猜你喜欢

热点阅读