我是Kotlin的新手,目前正在研究OOP的概念

我正在尝试使用此代码进行转换,但是我遇到了一个错误:

open class Operations1(){
open  fun sum(n1:Int , n2:Int):Int{
    return n1+ n2
}
fun sub(n1:Int , n2:Int):Int{
    return n1- n2
}
}
class multiOperations1():Operations(){
override  fun sum(n1:Int , n2:Int):Int{
    return n1+ n2 +5
}
fun mul(n1:Int , n2:Int):Int{
    return n1* n2
}
fun div(n1:Int , n2:Int):Int{
    return n1/ n2
}
}
fun main(args:Array<String>){

var oper = Operations()
var inlit  = multiOperations1() as Operations1
println("Enter first number")
var n1:Int = readLine()!!.toInt()
println("Enter Second Number")
var n2:Int = readLine()!!.toInt()

var sum = inlit.sum(n1 , n2)
var sub = inlit.sub(n1 , n2)
println("Sum: " + sum)
println("Sub: " + sub)
}

代码的屏幕截图

oop -  Actor 阵容永远不会成功-LMLPHP

错误:

oop -  Actor 阵容永远不会成功-LMLPHP

最佳答案

为此,可以使用Gson并避免样板代码。

var operation= Operations(....)

val json = Gson().toJson(operation)

val multiOperations:MultiOperations =Gson().fromJson(json, MultiOperations::class.java)

关于oop - Actor 阵容永远不会成功,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45310457/

10-13 04:25