【问题标题】:Instantiating type T in Kotlin在 Kotlin 中实例化类型 T
【发布时间】:2018-04-02 00:09:33
【问题描述】:

简而言之,我想省略下面示例中重复的getT()。我已经阅读了Instantiating a generic type in Kotlin,但是“将() -> T 作为参数”是什么意思?我怎样才能把它应用到下面?

interface Food
{
    var isHeated:Boolean;
    var name:String;
}

abstract class Cooker<T:Food>
{
    abstract fun getT():T;
    abstract fun enhance(t:T);
    fun cook(): T
    {
        var food = getT();
        food.isHeated = true;
        food.name = "heated " + food.name;
        enhance(food);
        return food;
    }
}

class PotatoChip:Food
{
    override var isHeated = false;
    override var name = "potato chip";
}

class PotatoChipCooker:Cooker<PotatoChip>()
{
    override fun getT(): PotatoChip {
        return PotatoChip();
    }
    override fun enhance(t:PotatoChip)
    {
        t.name = "salted " + t.name;
    }
}

class Pancake:Food
{
    override var isHeated = false;
    override var name = "pancake";
}

class PancakeCooker:Cooker<Pancake>()
{
    override fun getT(): Pancake {
        return Pancake();
    }
    override fun enhance(t:Pancake)
    {
        t.name = t.name + " coated with maple syrup";
    }
}

fun main(args: Array<String>)
{
    val result = PotatoChipCooker().cook();
    println(result.name);
    val result2 = PancakeCooker().cook();
    println(result2.name);
}

【问题讨论】:

    标签: kotlin


    【解决方案1】:

    您可以将初始化函数作为主构造函数的一部分。因此,实现类必须传递一个函数来指定如何创建相应的类型:

    abstract class Cooker<T : Food>(private val initT: () -> T) {
        abstract fun enhance(t: T)
        fun cook(): T {
            val food = initT()
            food.isHeated = true
            food.name = "heated $name"
            enhance(food)
            return food
        }
    }
    
    
    class PotatoChipCooker : Cooker<PotatoChip>({ PotatoChip() }) {
        override fun enhance(t: PotatoChip) {
            t.name = "salted ${t.name}"
        }
    }
    
    
    class PancakeCooker : Cooker<Pancake>({ Pancake() }) {
        override fun enhance(t: Pancake) {
            t.name = "${t.name} coated with maple syrup"
        }
    }
    

    请注意,我删除了可选的分号并使用了字符串模板而不是串联。此外,cook 方法可以简化为:

    fun cook() = initT().apply {
        isHeated = true
        name = "heated $name"
        enhance(this)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      相关资源
      最近更新 更多