Kotlin中構造方法的參數var val 和 什么都沒有的區別


1.什么都沒有,在該類中使不能使用的, 這個參數的作用就是,傳遞給父類的構造方法

2.使用var 可以在類中使用,相當於 我們聲明了一個該類中定義了一個private 的成員變量

3.val表示不讓修改該參數 加上了final 修飾符

 

class Glory (str:String , nom:Int){

} 轉為Java代碼

public
final class Glory { public Glory(@NotNull String str, int nom) { Intrinsics.checkParameterIsNotNull(str, "str"); super(); } }

 

class GloryHH(str: String, val nom: Int) 轉為kotlin代碼
public final class GloryHH {
   private final int nom;

   public final int getNom() {
      return this.nom;
   }

   public GloryHH(@NotNull String str, int nom) {
      Intrinsics.checkParameterIsNotNull(str, "str");
      super();
      this.nom = nom;
   }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM