当前位置: 首页 > news >正文

Android Developer的这段代码的注释(kotlin的类和对象

这段代码感觉很清晰,可以理一下,看项目应该会看得更懂(?

image

package com.github.rd227.helloworldimport kotlin.properties.ReadWriteProperty
import kotlin.reflect.KPropertyopen class SmartDevice(val name: String, val category: String) {var deviceStatus = "online"protected setopen val deviceType = "unknown"open fun turnOn() {deviceStatus = "on"}open fun turnOff() {deviceStatus = "off"}
}class SmartTvDevice(deviceName: String, deviceCategory: String) :SmartDevice(name = deviceName, category = deviceCategory) {override val deviceType = "Smart TV"private var speakerVolume by RangeRegulator(initialValue = 2, minValue = 0, maxValue = 100)private var channelNumber by RangeRegulator(initialValue = 1, minValue = 0, maxValue = 200)fun increaseSpeakerVolume() {speakerVolume++println("Speaker volume increased to $speakerVolume.")}fun nextChannel() {channelNumber++println("Channel number increased to $channelNumber.")}override fun turnOn() {super.turnOn()println("$name is turned on. Speaker volume is set to $speakerVolume and channel number is " +"set to $channelNumber.")}override fun turnOff() {super.turnOff()println("$name turned off")}
}class SmartLightDevice(deviceName: String, deviceCategory: String) :SmartDevice(name = deviceName, category = deviceCategory) {override val deviceType = "Smart Light"private var brightnessLevel by RangeRegulator(initialValue = 0, minValue = 0, maxValue = 100)fun increaseBrightness() {brightnessLevel++println("Brightness increased to $brightnessLevel.")}override fun turnOn() {super.turnOn()brightnessLevel = 2println("$name turned on. The brightness level is $brightnessLevel.")}override fun turnOff() {super.turnOff()brightnessLevel = 0println("Smart Light turned off")}
}class SmartHome(val smartTvDevice: SmartTvDevice,val smartLightDevice: SmartLightDevice
) {var deviceTurnOnCount = 0private setfun turnOnTv() {deviceTurnOnCount++smartTvDevice.turnOn()}fun turnOffTv() {deviceTurnOnCount--smartTvDevice.turnOff()}fun increaseTvVolume() {smartTvDevice.increaseSpeakerVolume()}fun changeTvChannelToNext() {smartTvDevice.nextChannel()}fun turnOnLight() {deviceTurnOnCount++smartLightDevice.turnOn()}fun turnOffLight() {deviceTurnOnCount--smartLightDevice.turnOff()}fun increaseLightBrightness() {smartLightDevice.increaseBrightness()}fun turnOffAllDevices() {turnOffTv()turnOffLight()}
}class RangeRegulator(initialValue: Int,private val minValue: Int,private val maxValue: Int
) : ReadWriteProperty<Any?, Int> {var fieldData = initialValueoverride fun getValue(thisRef: Any?, property: KProperty<*>): Int {return fieldData// property 可以获取属性的元数据(名字、类型等)//这里没用到}
/*
*
*
*    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {if (value in minValue..maxValue) {fieldData = value} else {// 用反射打印警告日志,包含属性名val className = thisRef?.javaClass?.simpleName ?: "Unknown"println("⚠️ [$className] 属性 '${property.name}' 赋值失败: $value 不在 [$minValue, $maxValue]")}//例如像这样反射访问,相当于把这个属性传进来,获取这个属性就是反射}
}
*
*
*/override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {if (value in minValue..maxValue) {fieldData = value// property 可以获取属性的元数据(名字、类型等)}}
}fun main() {var smartDevice: SmartDevice = SmartTvDevice("Android TV", "Entertainment")smartDevice.turnOn()smartDevice = SmartLightDevice("Google Light", "Utility")smartDevice.turnOn()
}

1.反射
2.类的继承调用

http://www.jsqmd.com/news/497558/

相关文章:

  • 如何评估大数据产品的用户满意度?
  • Day03——java基础语法
  • 多格式电子书阅读软件KOReader,你的阅读终极伴侣!
  • 低代码-无代码平台背后的开源技术
  • 免费降AI率的上限在哪?从技术角度分析效果天花板
  • 一行代码都不写?用AI工具10分钟生成一个Chrome插件
  • 小而强大的文件系统,大大提高微控制器的稳定性
  • 技术支持管理化技术服务台与事件管理
  • STM32:UART串口通信
  • 第八天(3.18)
  • 湖南特产酱板鸭项目有哪些
  • Rust Trait 对象动态分派原理
  • 身份认证方案
  • 去中心化应用(DApp)开发全流程
  • 计算机视觉算法优化
  • 【GUI-Agent】阶跃星辰 GUI-MCP 解读---(2)---决策层
  • 图像处理优化技巧
  • 如何设计一个安全的 RESTful API?
  • Python的__init_subclass__类方法在框架开发中的钩子机制与扩展点设计
  • 318记录
  • OpenClaw 目录结构详细介绍
  • 消息队列选型指南2024
  • JavaScript性能优化实战拿墩
  • SSH隧道实战:内网穿透与端口转发
  • Spring Boot 异步任务超时控制机制
  • Go Channel 死锁问题与调试
  • 旅游社交平台:用户生成内容与经验分享
  • TypeScript学习笔记 - P4
  • AI应用架构师实战:上下文理解增强方案的部署与运维
  • JAVA学习-Web基础2 分层解耦