# app-android **Repository Path**: archite/app-android ## Basic Information - **Project Name**: app-android - **Description**: No description available - **Primary Language**: Kotlin - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-17 - **Last Updated**: 2025-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 泛型 ## 泛型基础 泛型允许在类、接口和函数中使用类型参数,提供类型安全的同时避免类型转换。 ### 泛型类 // 定义泛型类 class Box(val item: T) { fun getItem(): T = item } // 使用泛型类 val intBox = Box(10) val stringBox = Box("Hello") // 类型推断 ### 泛型函数 // 泛型函数 fun singletonList(item: T): List { return listOf(item) } // 使用 val list = singletonList(42) ### 泛型约束 // 上界约束 fun > max(a: T, b: T): T { return if (a > b) a else b } // 多个约束 fun copyWhenGreater(list: List, threshold: T): List where T : Comparable, T : Number { return list.filter { it > threshold } } ### 型变(Variance) // 协变(out)- 生产者 interface Producer { fun produce(): T } // 逆变(in)- 消费者 interface Consumer { fun consume(item: T) } // 不变(invariant) class Container(var item: T) // 使用处型变 fun copy(from: Array, to: Array) { // from 只能读取 } ### 星投影 // 星投影表示未知类型 fun printList(list: List<*>) { for (item in list) { println(item) } } ### 具体化类型参数(reified) // 内联函数中使用 reified inline fun isInstance(value: Any): Boolean { return value is T } // 使用 val result = isInstance("Hello") // true # 不同类型的类 ## 枚举类 枚举类是一种特殊的类,用于定义一组有限的常量集合。每个枚举常量都是枚举类的一个实例。 ### 基本枚举类 enum class Direction { NORTH, SOUTH, EAST, WEST } // 使用 val direction = Direction.NORTH println(direction) // 输出: NORTH ### 带属性的枚举类 enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } // 使用 val color = Color.RED println(color.rgb) // 输出: 16711680 ### 带方法的枚举类 enum class Operation { ADD { override fun apply(x: Int, y: Int): Int = x + y }, SUBTRACT { override fun apply(x: Int, y: Int): Int = x - y }, MULTIPLY { override fun apply(x: Int, y: Int): Int = x * y }; abstract fun apply(x: Int, y: Int): Int } // 使用 val result = Operation.ADD.apply(5, 3) // 8 ### 枚举类的常用方法 enum class State { IDLE, RUNNING, FINISHED } // 获取所有枚举常量 val states = State.values() // 通过名称获取枚举常量 val state = State.valueOf("RUNNING") // 获取枚举常量的名称和位置 println(State.RUNNING.name) // RUNNING println(State.RUNNING.ordinal) // 1 ### 实现接口的枚举类 interface Printable { fun print() } enum class Priority : Printable { LOW { override fun print() = println("Low priority") }, MEDIUM { override fun print() = println("Medium priority") }, HIGH { override fun print() = println("High priority") } } ### 遍历枚举 // 遍历所有枚举值 for (direction in Direction.values()) { println(direction) } // 使用 enumValues() 和 enumValueOf() inline fun > printEnums() { enumValues().forEach { println(it) } } ## 数据类 数据类是专门用于存储数据的类,Kotlin 会自动为其生成一些有用的方法。 ### 基本数据类 data class User(val name: String, val age: Int) // 自动生成的方法: // - equals()/hashCode() // - toString() // - copy() // - componentN() 用于解构 // 使用 val user = User("Alice", 25) println(user) // 输出: User(name=Alice, age=25) ### 数据类的要求 // 1. 主构造函数至少有一个参数 // 2. 主构造函数的参数必须标记为 val 或 var // 3. 数据类不能是 abstract、open、sealed 或 inner ### 复制对象 data class Person(val name: String, val age: Int, val city: String) val person1 = Person("Bob", 30, "Beijing") // 使用 copy() 创建副本,可以修改部分属性 val person2 = person1.copy(age = 31) println(person2) // Person(name=Bob, age=31, city=Beijing) ### 解构声明 data class Point(val x: Int, val y: Int) val point = Point(10, 20) // 解构声明 val (x, y) = point println("x = $x, y = $y") // x = 10, y = 20 // 在循环中使用 data class Employee(val name: String, val salary: Int) val employees = listOf( Employee("Alice", 5000), Employee("Bob", 6000) ) for ((name, salary) in employees) { println("$name earns $salary") } ### 数据类继承 // 数据类可以继承接口 interface Named { val name: String } data class Student( override val name: String, val grade: Int ) : Named // 数据类可以继承其他类(但该类必须是 open 或 abstract) open class Base(val id: Int) data class Derived(val name: String, val baseId: Int) : Base(baseId) ### equals 和 hashCode data class Book(val title: String, val author: String, val year: Int) val book1 = Book("Kotlin in Action", "Dmitry", 2017) val book2 = Book("Kotlin in Action", "Dmitry", 2017) val book3 = book1 println(book1 == book2) // true (内容相等) println(book1 === book2) // false (引用不同) println(book1 === book3) // true (引用相同) // hashCode 相同 println(book1.hashCode() == book2.hashCode()) // true ### 排除属性 // 类体中声明的属性不会参与 equals/hashCode/toString/copy data class Account(val username: String, val email: String) { var lastLogin: Long = 0 // 不参与数据类的自动生成方法 } val account1 = Account("user1", "user1@example.com") val account2 = Account("user1", "user1@example.com") account2.lastLogin = System.currentTimeMillis() println(account1 == account2) // true (lastLogin 不参与比较) ### 数据类与集合 data class Product(val id: Int, val name: String, val price: Double) val products = listOf( Product(1, "Laptop", 999.99), Product(2, "Mouse", 29.99), Product(3, "Keyboard", 79.99) ) // 使用解构 products.forEach { (id, name, price) -> println("Product #$id: $name - $price") } // 在 Map 中使用 val productMap = products.associateBy { it.id } println(productMap[1]) // Product(id=1, name=Laptop, price=999.99) # 单列对象和伴生对象 # 扩展属性和函数 # 作用域