我想要这样的东西:
using System.Linq;
仅当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))
}
}
}
}
不为空时,我才想覆盖函数appendTooltip
。
答案 0 :(得分:1)
(免责声明:我不是Kotlin用户,所以我的语法可能是错误的-但相同的原则适用于任何OOP语言):
Kotlin仍然使用JVM,并且JVM有自己的虚拟调用系统,该系统不像您描述的那样支持运行时调度-但这不是必需的功能,因为您可以在覆盖内添加保护声明(即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 ) )
}
}
}
我想我认为您实际上打算使用check
方法来执行以下操作:
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 )
}
}
}