Tag: 代表团

Kotlin – 如何使用自定义名称制作地图代理?

我试图让我的头绕着财产代表,我有一个有趣的用例。 有没有可能有这样的事情: class MyClass { val properties = mutableMapOf() val fontSize: Any by MapDelegate(properties, “font-size”) } 这将允许我使用地图作为fontSize存储fontSize ,但使用自定义键(即“font-size”)。 如果用于存储诸如可以通过variables访问的CSS属性标记( fontSize )以用于代码的特定用例,但是在迭代地图( font-size: 18px; )时可以正确呈现。

对象的快速inheritance和接口的含义

我在Kotlin中find:Object文档的一个例子: open class A(x: Int) { public open val y: Int = x } interface B {…} val ab: A = object : A(1), B { override val y = 15 } 所以我用更有意义的名字实现了这个例子,我不知道在逗号分隔的超types列表之间的接口的原因是什么? interface Toy { fun play () { println(“Play, play….”) } } open class Ball(public open val color: String = “red”) {} val […]

我如何将实现委托给Kotlin中的可变属性?

至于我的理解,在Kotlin中委托一个实现的想法是避免看起来像这样的代码: class MyClass(val delegate : MyInterface) : MyInterface { override fun myAbstractFun1() = delegate.myAbstractFun1() override fun myAbstractFun2() = delegate.myAbstractFun2() // … } 相反,我们可以写下面的代码,它应该这样做: class MyClass(val delegate : MyInterface) : MyInterface by delegate 现在,我想delegate是一个可变的variables,即我的代码如下所示: var delegate : MyInterface = MyImplementation() object MyObject : MyInterface by delegate 所以,如果我像第一个例子一样委托每个抽象方法来delegate自己的delegate那么改变delegate的值确实会改变方法的行为。 但是,上面的代码编译为这个Java代码: public final class MyObject implements MyInterface { public […]

我可以使用Java扩展一个Kotlin授权类吗?

我想在Java中扩展一个Kotlin委托类,并得到以下错误: 不能从最终的“派生” 看下面的代码。 我想要做的是装饰一个类的方法。 任何想法为什么Kotlin定义Derived为最终? 有没有办法Derived不是最终的,所以我可以inheritance它? Java的: new Derived(new BaseImpl(10)) { // Getting the error on this line: `Cannot inherit from final ‘Derived’` }; 科特林: interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b *从这里的例子: https : […]

Kotlin透明属性解析器?

代码比单词简单: data class Dim2(val x:Int , val y:Int) data class Dim3(val dim2:Dim2 , val z:Int) fun main(args: Array) { val v = Dim3(Dim2(1,2) , 3) println(“value : x = ${v.dim2.x} , y = ${v.dim2.y} , z = ${vz} “) } 因为Kotlin主张inheritance的构成,所以我把Dim2写成Dim3 。 但在Dim3 ,访问Dim2的属性非常难看。 不如inheritance优雅。 解决这个问题的一个办法是这样的: interface IDim2 { val x:Int val y:Int } interface […]