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;
   }
}

 

posted @ 2019-03-06 19:43  GLORY-HOPE  阅读(1382)  评论(0编辑  收藏  举报