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
相关推荐
Digitally9 小时前
如何用5种实用方法将电脑上的音乐传输到安卓手机
android·智能手机·电脑
HahaGiver66610 小时前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
2501_9159090610 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
IT乐手11 小时前
android 下载管理工具类
android
2501_9151063212 小时前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
科技峰行者13 小时前
安卓16提前发布能否改写移动生态格局
android
蒲公英少年带我飞14 小时前
Android NDK 编译 protobuf
android
沐怡旸14 小时前
【底层机制】ART虚拟机深度解析:Android运行时的架构革命
android·面试
小禾青青14 小时前
uniapp安卓打包遇到报错:Uncaught SyntaxError: Invalid regular expression: /[\p{L}\p{N}]/
android·uni-app
studyForMokey15 小时前
【Kotlin内联函数】
android·开发语言·kotlin