Kotlin 主构造函数

一、定义与基本声明

  1. 类头声明

    主构造函数直接定义在类名后,形式为 class 类名(参数列表)。若无需可见性修饰或注解,可省略 constructor 关键字‌12。

    kotlin 复制代码
    class Person(name: String)  // 省略 constructor
    class Student private @Inject constructor(id: Int)  // 需显式声明 constructor‌:ml-citation{ref="4,5" data="citationList"}
  2. 无显式构造时的默认行为

    若未声明任何构造函数,Kotlin 自动生成无参主构造函数;若已声明主构造或次构造,则不再生成默认构造‌13。


二、初始化逻辑与执行顺序

  1. init 代码块

    主构造函数本身无函数体,初始化逻辑需通过 init{} 代码块实现。多个 init 块按声明顺序执行,且与属性初始化器穿插运行‌16。

    kotlin 复制代码
    class Demo(name: String) {
        val prop1 = "Prop1: $name".also(::println)  // 属性初始化
        init { println("First init block") }        // 初始化块1
        init { println("Second init block") }       // 初始化块2
    }
  2. 执行顺序

    主构造函数初始化(含属性赋值和 init 块)优先于次构造函数逻辑‌13。


三、主构造函数中声明属性

  1. 直接声明成员属性

    在主构造参数前添加 val/var 可自动生成同名成员属性,无需手动赋值‌23。

    kotlin 复制代码
    class User(val name: String, var age: Int)  // 自动生成 name(只读)和 age(可写)
  2. 临时变量与手动赋值

    若未用 val/var 修饰参数,需通过 init 块或属性初始化器手动赋值‌37。

    arduino 复制代码
    class Book(_title: String) {
        val title: String
        init { title = _title.uppercase() }
    }
kotlin 复制代码
// 主构造函数:规范来说,都是增加_xxx的方式,临时的输入类型,不能直接用,需要接收下来 成为变量才能用
// _name 等等,都是临时的类型,不能直接要弄,需要转化一下才能用
class KtBase71(_name: String, _sex: Char, _age: Int, _info: String) // 主构造函数
{
  var name = _name
      get() = field // get不允许私有化
      private set(value) {
          field = value
      }

  val sex = _sex
      get() = field
  // set(value) {} 只读的,不能修改的,不能set函数定义

  val age: Int = _age
      get() = field + 1

  val info = _info
      get() = "【${field}】"

  fun show() {
      // println(_name) 临时的输入类型,不能直接用,需要接收下来 成为变量才能用
      println(name)
      println(sex)
      println(age)
      println(info)
  }
}

fun main(){
 val ktBase71 = KtBase71(_name = "Lilei",_age = 18,_sex = 'M',_info = "这里是简介")
  ktBase71.show()
}

四、可见性修饰与注解

  1. 可见性控制

    主构造函数可添加 privateprotected 等修饰符限制访问权限‌45。

    kotlin 复制代码
    class Singleton private constructor()  // 单例模式,禁止外部构造
  2. 注解支持

    主构造函数支持使用 @Inject 等注解标记依赖注入场景‌45。


五、与次构造函数的关系

  1. 委托调用

    次构造函数必须通过 this() 显式或隐式委托给主构造函数(或另一已委托的次构造)‌38。

    javascript 复制代码
    class View {
        constructor(ctx: String) : this(ctx, "default")  // 委托给双参次构造
        constructor(ctx: String, attr: String) { /*...*/ }
    }

总结

特性 实现方式 示例
基本声明 类名后直接定义参数列表,可省略 constructor class Person(name: String)‌25
属性自动生成 使用 val/var 修饰构造参数 class User(val id: Int)‌35
初始化逻辑 通过 init{} 代码块实现,支持多块顺序执行 init { require(age > 0) }‌67
可见性控制 添加 privateprotected 等修饰符 private constructor()‌45
次构造委托 次构造函数通过 this() 委托主构造或其他次构造 constructor(name: String) : this(name, 0)‌8
相关推荐
消失的旧时光-194320 小时前
从 Android 组件化到 Flutter 组件化
android·flutter·架构
Android轮子哥20 小时前
Android 12 SplashScreen 一种另类的适配方案
android·github
粤M温同学21 小时前
Android OkHttp 下载限速方案实现
android·okhttp
2501_915106321 天前
Perfdog 成本变高之后,Windows 上还能怎么做 iOS APP 性能测试
android·ios·小程序·https·uni-app·iphone·webview
愤怒的代码1 天前
从开发调试到生产上线:全维度 Android 内存监控与分析体系构建
android·java·kotlin
jzlhll1231 天前
Android最简化发布模块到mavenCentral
android·mavencentral
2501_915106321 天前
iOS 安装了证书,HTTPS 还是抓不到
android·网络协议·ios·小程序·https·uni-app·iphone
好奇龙猫1 天前
【人工智能学习-AI-MIT公开课13.- 学习:遗传算法】
android·人工智能·学习
TO_ZRG1 天前
Unity打包安卓、iOS知识点
android·unity·android studio
周杰伦fans1 天前
AndroidStudioJava国内镜像地址gradle
android·java·android-studio