在下面的代码中,我想开发一个我在谷歌上看到的例子。但是当我尝试打电话时
.value
它在 android studio 中从未被识别或定义。请看看,让我知道如何解决这个问题
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var opAdd = Op1.add(5)
var opSub = Op1.sub(6)
var opMul = Op1.mul(7)
var opDiv = Op1.div(8)
exec(3, opAdd)
exec(3, opSub)
exec(3, opMul)
exec(3, opDiv)
}
fun exec(operand : Int, op : Op1) = when (op) {
is Op1.add() -> operand + op.value//ERROR
is Op1.sub -> operand - op.value//ERROR
is Op1.mul -> operand * op.value
is Op1.div -> operand / op.value
}
}
密封类:
public sealed class Op1 {
class add(val oAdd : Int) : Op1()
class sub(val oSub : Int) : Op1()
class mul(val oMul : Int) : Op1()
class div(val oDiv : Int) : Op1()
}