kotlin状态模式
作者:
腊鸭Laya | 来源:发表于
2018-11-19 10:11 被阅读4次/**
* 状态模式
*/
interface State {
fun shopping()
fun move()
}
class Loving : State {
override fun move() {
println("一起看电影")
}
override fun shopping() {
println("一起看逛街")
}
}
class NoLove : State {
override fun move() {
println("不看电影")
}
override fun shopping() {
println("一个人逛街")
}
}
class Context2 {
private var mState: State? =null
private fun setState(state: State) {
mState = state
}
fun inLove() {
setState(Loving())
}
fun outLove() {
setState(NoLove())
}
fun move() {
mState!!.move()
}
fun shopping() {
mState!!.shopping()
}
}
fun main(args: Array) {
val context = Context2()
context.inLove()
context.move()
context.shopping()
context.outLove()
context.move()
context.shopping()
}
本文标题:kotlin状态模式
本文链接:https://www.haomeiwen.com/subject/abrofqtx.html
网友评论