Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/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 - Fatal编程技术网

Kotlin 仅当满足条件时才重写功能

Kotlin 仅当满足条件时才重写功能,kotlin,Kotlin,我想要这样的东西: class ItemBase(private val TOOLTIP: String) : Item(Settings().group(EnderIO.ENDERIO)) { fun check() { if (TOOLTIP.isNotBlank()) { override fun appendTooltip(itemStack: ItemStack?, world: World?, tooltip: MutableList&

我想要这样的东西:

class ItemBase(private val TOOLTIP: String) : Item(Settings().group(EnderIO.ENDERIO)) {
    fun check() {
        if (TOOLTIP.isNotBlank()) {
            override fun appendTooltip(itemStack: ItemStack?, world: World?, tooltip: MutableList<Text?>, tooltipContext: TooltipContext?) {
                tooltip.add(TranslatableText(TOOLTIP))
            }
        }
    }
}
class ItemBase(
    private var appendTooltipOverridden: Boolean
    private val TOOLTIP: String
) : Item( Settings().group( EnderIO.ENDERIO ) ) {

    fun check() {
        if( this.TOOLTIP.isNotBlank() ) {
            this.appendTooltipOverridden = true;
        }
    }

    override fun appendTooltip( itemStack: ItemStack?, world: World?, tooltip: MutableList<Text?>, tooltipContext: TooltipContext? ) {

        if( this.appendTooltipOverridden ) {
            tooltip.add( TranslatableText( TOOLTIP ) )

        }
        else {
            // Pass-through to the base implementation:
            super.appendTooltip( itemStack, world, tooltip, tooltipContext )
        }
    }
}
class ItemBase(私有val工具提示:字符串):项(设置().group(EnderIO.EnderIO)){
趣味支票{
if(TOOLTIP.isNotBlank()){
覆盖工具提示(itemStack:itemStack?、world:world?、工具提示:可变列表、工具提示上下文:工具提示上下文?){
添加(可翻译文本(工具提示))
}
}
}
}
仅当
工具提示
不为空时,我才想覆盖函数
appendTooltip

(免责声明:我不是Kotlin用户,因此我的语法可能是错误的-但相同的原则适用于任何OOP语言):

Kotlin仍然使用JVM,JVM有自己的虚拟调用系统,它不支持如您所描述的运行时调度-但这不是一个需要的功能,因为您只需在覆盖中添加guard语句(即
if
检查),然后在适当时调用
super
版本。这是OOP最基本和最基本的部分之一,也是所有OOP语言中的一个特性

基本上,这样做:

class ItemBase(
    private val TOOLTIP: String
) : Item( Settings().group( EnderIO.ENDERIO ) ) {

    override fun appendTooltip( itemStack: ItemStack?, world: World?, tooltip: MutableList<Text?>, tooltipContext: TooltipContext? ) {

        if( TOOLTIP.isNotBlank() ) {
            super.appendTooltip( itemStack, world, tooltip, tooltipContext )
        }
        else {
            tooltip.add( TranslatableText( TOOLTIP ) )
        }


    }
}

“总是覆盖”怎么办?但您的自定义操作只有在满足条件的情况下才会执行?我不能,
AppendTooltip
函数来自一个库,为什么不呢?如果条件不满足,调用超级函数,无论出于何种目的,它都好像不满足一样overriden@Hunam这就是
super
关键字的作用。你能给我发一封信吗(我是Kotlin初学者)?
check
方法就是我想让它工作的方法。我不想向类中添加其他参数。我只需要一个参数,一个字符串,我想检测这个字符串是否为空,然后,如果字符串不为空,则重写函数,如果不是,则什么都不做。你知道@Dai吗?@Hunam我的第一个示例就是这样做的。你还不明白什么?您是否了解
super
调用的工作原理?