Tag: 接口

如何实例化在Kotlin中实现接口的匿名类

在Java中,实例化一个接口对象就像new Interface()一样简单,并覆盖所有必需的函数,如下所示,在AnimationListener private void doingSomething(Context context) { Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); animation.setAnimationListener(new Animation.AnimationListener() { // All the other override functions }); } 但是,当我们键入Kotlin时 private fun doingSomething(context: Context) { val animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in) animation.setAnimationListener(Animation.AnimationListener(){ // All the other override functions }) } 它的错误投诉未解决参考AnimationListener。

对象的快速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 […]

在实现它的类中设置接口的setter

刚开始在android中使用kotlin- 我正在尝试在实现它的类中使用接口的setter – interface MyInterface { val prop: Int // abstract var propertyWithImplementation: String get() = “foo” set(text){“$text foo”} fun foo() { print(prop) } } class Child : MyInterface { override val prop: Int = 29 override var propertyWithImplementation=”bhu” } fun main(args: Array) { println(Child().propertyWithImplementation) } 输出:BHU 预期产出= bhu foo 我哪里错了?

Java不允许最终的默认方法..但是Kotlin?

如果您尝试在界面上创建内联函数,则会出现错误: ‘inline’ modifier is not allowed on virtual members. Only private or final members can be inlined ‘inline’ modifier is not allowed on virtual members. Only private or final members can be inlined 我明白,这是因为该function是虚拟的,因为它可以被覆盖。 如果我们能够声明“关闭”function,这些function将不是虚拟的,因此可以内联,这是非常有用的! 使用“私人”给我们一个非虚拟的“封闭”function,但是世界其他地方却不能使用它! 所以..有没有办法为抽象types定义“封闭的”非虚拟可嵌入函数? ( ps我打算自己回答这个问题,但可以自由分享你自己的答案! )

任何方式在Kotlin中从相同的通用接口inheritance两次(使用单独的types)?

我在我的代码中有一个场景,我想要一个类实现两个不同types的接口,如下面的示例: interface Speaker { fun talk(value: T) } class Multilinguist : Speaker, Speaker { override fun talk(value: String) { println(“greetings”) } override fun talk(value: Float) { // Do something fun like transmit it along a serial port } } 科特林对此并不满意,理由是: Type parameter T of ‘Speaker’ has inconsistent values: kotlin.String, kotlin.Float A supertype appears twice 我知道一个可能的解决方案是实现下面的代码,在那里我用实现接口,然后检查自己的types并将它们委托给它们的函数。 […]

在Kotlin中声明静态接口字段

是否有可能编写Java的等价物 interface Foo { public static final INSTANCE = new Foo {}; } 在Kotlin? 如果Foo是一个class,我可以使用: class Foo { companion object { @JvmField val INSTANCE = object : Foo() {} } } 但与一个接口,它给出了一个错误: JvmField无法应用于接口的伴随对象中定义的属性 @JvmStatic也不起作用。

使用String name()声明实现Java接口的Kotlin枚举

我有一个Kotlin项目,我使用Java库依赖项来定义一个带有String name()方法声明的接口。 在Java中,我可以在枚举声明中使用此接口,其中String name()方法由枚举隐式实现。 public interface Aspect { int index(); String name(); } 在Java中这是可能的: public enum CollisionType implements Aspect { ONE, TWO, THREE; private final Aspect aspect; private CollisionType() { aspect = CONTACT_ASPECT_GROUP.createAspect(name()); } @Override public int index() { return aspect.index(); } } 如果我在一个Kotlin枚举类中尝试这个,由于冲突的名字“name”,我得到一个[冲突的遗留的JVM DECLARATIONS]错误。 我试图使用@JvmName注释来定义一个不同的名称,因为这种types的问题是建议做的,但是我无法正确使用这个问题。 enum class CollisionType : Aspect { ONE, TWO, TREE; […]

Kotlin属性不能被子接口覆盖

在下面的精简的例子中,你能解释为什么Kotlin编译器在覆盖一个我们进一步限制它的types的时候会抱怨(编译器消息: Var-property type is ‘B’, which is not a type of overriden public abstract var a: A ) interface A interface B : A { fun someFunc():Boolean } class C : B { override fun someFunc(): Boolean { return true } } abstract class D { abstract var a: A } class E : D() […]

Lambda在kotlin中的接口实现

kotlin中的代码会是什么样子,似乎没有什么工作我尝试: public interface AnInterface { void doSmth(MyClass inst, int num); } 在里面: AnInterface impl = (inst, num) -> { //… }

通过使用在超类中定义的相同名称的var来覆盖在接口中定义的val

有一个基类( var需要保护): open class Base(protected var id: Int) {} 有一个需要使用val的接口: interface ProviderI { val id: Int } 还有一个从Baseinheritance的类,实现了ProviderI接口。 其中我试图使用超类var (具有相同的名称)实现接口的val 。 作为一个不工作的例子,我试图做这样的事情(例如不起作用): class Instance(id: Int): Base(id), ProviderI { override val id get() { return super.id } } 这个想法很简单,但我已经尝试了一切,每次都会报告不同的错误。