Tag: 接口

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

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

如何创建一个接口的匿名实现?

我有一个接口: interface TileSet { fun contains(x: Int, y: Int) : Boolean } 我想能够创建瓦片集合(瓦片是一对x和y整数坐标): fun TileSet.union(another: TileSet) : TileSet = // .. 在Java 8中,我可以这样做: @FunctionalInterface public interface TileSet { boolean contains(int x, int y); public default TileSet unite(TileSet another) { return (x, y) -> TileSet.this.contains(x, y) && another.contains(x, y); } } 所以一个接口是用TileSet#unite()的lambda实现的。 或者可以用旧的匿名类方法来实现: public default TileSet […]

编写一个比Java更优雅的可复制接口

我试图编写一个接口,类可以实现,使他们“可复制”,(类型)安全Clonable。 在Java中,我会使用递归泛型来做这样的事情: public interface Copyable<C extends Copyable<C>> { C copy(); } public class Example implements Copyable<Example> { … @Override public Example copy() { return new Example(this); //invoke copy constructor } } 显然这不是那么优雅,可Copyable和Example的标题看起来过于复杂。 有没有更好的方式来实现这个Kotlin?

在Kotlin中匿名实现接口导致“没有构造函数”错误

我想在Android中使用SurfaceView来保存Camera预览。 文档告诉我,我需要调用表面保持器的surfaceCreated回调startPreview。 我试图设置回调像这样 this.surface!!.holder!!.addCallback(SurfaceHolder.Callback() { fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { } fun surfaceCreated(holder: SurfaceHolder) { } fun surfaceDestroyed(holder: SurfaceHolder) { } }) 但是,我得到的错误: SurfaceHolder.Callback没有构造函数。 我很困惑,为什么这样做不起作用,当这样做: Thread(Runnable() { fun run() { … } })

Kotlin是否允许一个函数返回这个接口的实现类型的值?

我想写这样的东西: /** * Represents a state, large or small. */ interface State { /** * */ val changeValue: StateChange<This> // does not compile; "This" is a keyword I made up that might reference the implementing class } /** * Represents a change in a state, large or small. * * @param StateType The type of […]

是否可以在Kotlin界面中指定一个静态函数?

我想要做这样的事情: interface Serializable<FromType, ToType> { fun serialize(): ToType companion object { abstract fun deserialize(serialized: ToType): FromType } } 甚至这会对我有用: interface Serializable<ToType> { fun serialize(): ToType constructor(serialized: ToType) } 但是都没有编译。 有没有这样的语法,或者我会被迫使用这个接口的工厂? 还是有另一个答案? 😮那会很整洁!

在Kotlin中,当枚举类实现接口时,如何解决继承的声明冲突?

我定义了一个实现Neo4j的RelationshipType的枚举类: enum class MyRelationshipType : RelationshipType { // … } 我得到以下错误: Inherited platform declarations clash: The following declarations have the same JVM signature (name()Ljava/lang/String;): fun <get-name>(): String fun name(): String 据我所知, Enum类的name()方法和RelationshipType接口的name()方法都有相同的签名。 这在Java中并不是一个问题,那么为什么在Kotlin中是错误的,我该如何解决它呢?

Kotlin与身体的抽象方法

如上所述:如果接口中的函数没有主体,则默认为抽象。 但是接口的功能与身体无关。 例: interface MyInterface { fun foo() { print("Something") } fun bar() } fun main(args: Array<String>) { println(MyInterface::foo.javaMethod) println(MyInterface::bar.javaMethod) } 输出将是: public abstract void MyInterface.foo() public abstract void MyInterface.bar() 如何可能,具有定义身体的方法是抽象的?

在Kotlin lambda表达式中重写多个接口方法

说我有两个方法onCurrentLocation和onError的Callbacks接口: public interface Callbacks { void onCurrentLocation(ClientLocation location); void onError(); } 和一个在它的构造函数中接受这个接口的类,说: public MyLocationClient(Callbacks callbacks) {…} 现在,在Kotlin中,我应该如何能够实例化MyLocationClient: val myLocationClient = MyLocationClient( { location: ClientLocation -> updateClientLocation(location) }, {}) 如果没有,为什么不呢? 我看到的行为是:当接口只有一个方法,这个对象的构造罚款。 但是,只要我添加更多的方法Callbacks ,编译器抱怨 “Type mismatch。Required:Callbacks!Found:(ClientLocation) – > Unit” 编辑:删除空位检查的location因为它是不相关的问题。

如何实例化在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。