Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin中的运行时多态性_Kotlin_Polymorphism_Overriding - Fatal编程技术网

Kotlin中的运行时多态性

Kotlin中的运行时多态性,kotlin,polymorphism,overriding,Kotlin,Polymorphism,Overriding,在这种情况下有没有优雅的方法来应用多态性?解析器在运行时提供以下类: class io.swagger.v3.oas.models.media.Schema //is parent of the rest : class io.swagger.v3.oas.models.media.ComposedSchema class io.swagger.v3.oas.models.media.ArraySchema class io.swagger.v3.oas.models.media.String

在这种情况下有没有优雅的方法来应用多态性?解析器在运行时提供以下类:

class io.swagger.v3.oas.models.media.Schema //is parent of the rest :

class io.swagger.v3.oas.models.media.ComposedSchema
class io.swagger.v3.oas.models.media.ArraySchema
class io.swagger.v3.oas.models.media.StringSchema
class io.swagger.v3.oas.models.media.ObjectSchema
我希望每个类都有相同名称的函数和简单、简短的方法,这些方法将在运行时强制转换和调用必要的函数。这实际上正在发生,但我希望有更简单的解决方案,而无需制作此类副本:

fun main() {

    val parser = OpenAPIV3Parser()
    val asList = listOf(pathYaml3, pathYml2)
    val map = asList.map(parser::read)
            .flatMap { it.components.schemas.values }
            .forEach(::parseRawSchema)
}


fun parseRawSchema(schema: Schema<Any>) {

    if (schema is ComposedSchema) {
        parseSchema(schema)
    }
    if (schema is StringSchema) {
        parseSchema(schema)
    }
...
}

fun parseSchema(schema: ComposedSchema) {
    println("Compose-schema")
}

fun parseSchema(schema: StringSchema) {
    println("Sting-schema")
}

...

fun main(){
val parser=OpenAPIV3Parser()
val asList=listOf(pathymal3,pathYml2)
val map=asList.map(解析器::read)
.flatMap{it.components.schemas.values}
.forEach(::parseRawSchema)
}
模式(模式:模式){
if(模式为ComposedSchema){
解析模式(模式)
}
if(模式为StringSchema){
解析模式(模式)
}
...
}
fun parseSchema(模式:ComposedSchema){
println(“组合模式”)
}
模式(模式:StringSchema){
println(“Sting模式”)
}
...
尝试使用。 例如:

fun ComposedSchema.parseSchema() {
    println("Compose-schema")
}

fun StringSchema.parseSchema() {
    println("Sting-schema")
}

除此之外:

fun parseRawSchema(schema: Schema<Any>) {
    schema.parseSchema()
}

schema(schema:schema){
schema.parseSchema()
}
尝试使用。 例如:

fun ComposedSchema.parseSchema() {
    println("Compose-schema")
}

fun StringSchema.parseSchema() {
    println("Sting-schema")
}

除此之外:

fun parseRawSchema(schema: Schema<Any>) {
    schema.parseSchema()
}

schema(schema:schema){
schema.parseSchema()
}

不幸的是,在为
schema.class
创建扩展之前,我无法在
schema
上调用此扩展函数,但当我这样做时,程序将只使用它。我也无法在此扩展中添加任何
覆盖
修改器。但不管怎么说,这是一个有趣的方法,可能是我更进一步了。谢谢不幸的是,在为
schema.class
创建扩展之前,我无法在
schema
上调用此扩展函数,但当我这样做时,程序将只使用它。我也无法在此扩展中添加任何
覆盖
修改器。但不管怎么说,这是一个有趣的方法,可能是我更进一步了。谢谢