kotlin 确保操作只能在特定条件下执行?

ux6nzvsh  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(236)

我是Kotlin的新手,我一直在想,如果满足某个条件,我怎么能告诉程序它只能执行某些功能。比如,如果电视打开,你只能增加TvVolume,或者如果电视打开,你只能改变频道。这是Kotlin课程的练习,我被卡住了,或者只是想多了。下面是我的代码:(我试图在SmartHome类中实现那个条件)

`  

    open class SmartDevice(val name: String, val category: String) {

    var deviceStatus = "online"
        protected set

    open val deviceType = "unknown"

    open fun turnOn() {
        deviceStatus = "on"
    }

    open fun turnOff() {
        deviceStatus = "off"
    }
    
    open fun printDeviceInfo() {
        
        println("Device name: $name, category: $category, type: $deviceType")
    }
    
    }

    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")
    }
    
    fun decreaseVolume() {
       speakerVolume--
       println("Speaker volume decreased to $speakerVolume")
       
    }   
    
    fun previousChannel() {
        channelNumber--
        println("Channel number decreased to $channelNumber")
    }
    }

    
    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 = 2
        println("$name turned on. The brightness level is $brightnessLevel.")
    }

    override fun turnOff() {
        super.turnOff()
        brightnessLevel = 0
        println("Smart Light turned off")
    }
    
    fun decreaseBrightness() {
        brightnessLevel--
        println("Brightness decreased to $brightnessLevel")
    }
    
    }

    class SmartHome(
    val smartTvDevice: SmartTvDevice,
    val smartLightDevice: SmartLightDevice
    ) {

    var deviceTurnOnCount = 0
        private set

    fun turnOnTv() {
        deviceTurnOnCount++
        smartTvDevice.turnOn()
    }

    fun turnOffTv() {
        deviceTurnOnCount--
        smartTvDevice.turnOff()
    }

    fun increaseTvVolume(deviceStatus -> "on") {
        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 = initialValue

    override fun getValue(thisRef: Any?, property: KProperty<*>): Int {
        return fieldData
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
        if (value in minValue..maxValue) {
            fieldData = value
        }
    }
    }

    fun main() {
    var smartDevice: SmartDevice = SmartTvDevice("Android TV", "Entertainment")
    smartDevice.turnOn()

    smartDevice = SmartLightDevice("Google Light", "Utility")
    smartDevice.turnOn()
    smartDevice.decreaseBrightness()
    
  
    smartDevice = SmartTvDevice("Android TV", "Entertainment" )
    smartDevice.printDeviceInfo()
    smartDevice.increaseSpeakerVolume()
    smartDevice.increaseSpeakerVolume()
    smartDevice.increaseSpeakerVolume()
    smartDevice.increaseSpeakerVolume()
    smartDevice.increaseSpeakerVolume()
    smartDevice.decreaseVolume()
     
    }

` }

字符串
我肯定想多了
问题是,如果deviceStatus不是“on”,我希望所有这些操作都变得无法实现

yftpprvb

yftpprvb1#

尝试以下步骤:
创建函数isDeviceOn(),返回Boolean

open fun isDeviceOn() : Boolean {
    return this.deviceStatus == "online" || this.deviceStatus == "on"
}

字符串
使用此函数,您可以检查deviceStatus是否等于"online""on"。如果满足这些条件之一,则函数返回true,否则返回false
如果需要从类内部检查设备是否打开 *,可以直接使用

if (this.isDeviceOn()) {
    // Device is on
} else {
    // Device is off
}


如果您有SmartDevice**示例,则可以使用

val device = SmartDevice(...)
if (device.isDeviceOn()) {
     // Device is on
} else {
     // Device is off
}

oxf4rvwz

oxf4rvwz2#

正如我所说的,我想多了。我用一个简单的if语句做了我想做的事情:

` fun increaseSpeakerVolume() {
            if (deviceStatus == "on") {
            speakerVolume++
            println("Speaker volume increased to $speakerVolume.")
            }
        }`

字符串
我对每个函数都做了。当我运行它的时候:

`smartDevice.turnOff(); smartDevice.increaseSpeakerVolume()`


我明白了:
Android TV关闭
当我跑步时:

smartDevice.turnOn(); smartDevice.increaseSpeakerVolume()


我明白了:
Android TV已打开。扬声器音量设置为1,频道号设置为1。扬声器音量增加到2。

相关问题