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