简体   繁体   English

在java中实现kotlin接口

[英]Implementing a kotlin interface in java

So, after this question where I basically exploits reflection for passing primitive references to modify the primitive itself, like: 所以,在这个问题之后 ,我基本上利用反射传递原始引用来修改原语本身,如:

_begin("Another Window", ::showAnotherWindow)

I was looking for something to make something similar possible also from java, where at the moment I am using plains primitive arrays: 我正在寻找一些东西来制作类似的东西也可以从java,目前我正在使用平原原始数组:

private boolean[] showAnotherWindow = {false};
imgui.begin("Another Window", showAnotherWindow);

@hotkey suggested me the possibility to create a class implementing the KMutableProperty0 interface and that automatically gets and sets the corresponding variable @hotkey建议我创建一个实现KMutableProperty0接口的类,并自动获取和设置相应的变量

Example: 例:

KMutableProperty0<Boolean> prop = 
  PropUtils.javaProp(this, t -> t.showAnotherWindow, (t, r) -> { t.showAnotherWindow = r; });

_begin("Another Window", prop);

So, I wanted to give it a try and implemented the following in java. 所以,我想尝试一下并在java中实现以下内容。

Getter : 吸气

@FunctionalInterface
public interface Getter<T> {

    T get();
}

Setter : 塞特

@FunctionalInterface
public interface Setter<T> {

    void set(T type);
}

And then the class itself (I just wrote the constructor, all the methods are those requested by the interface and automatically implemented by the IDE) : 然后是类本身 (我只是编写了构造函数,所有方法都是接口请求的并由IDE自动实现):

public class JavaProp <T> implements KMutableProperty0<T> {

    private imgui.Getter<T> getter;
    private imgui.Setter<T> setter;

    public JavaProp(imgui.Getter<T> getter, imgui.Setter<T> setter) {
        this.getter = getter;
        this.setter = setter;
    }

    @Override
    public void set(T t) {
        setter.set(t);
    }

    @NotNull
    @Override
    public Setter<T> getSetter() {
        return null;
    }

    @Override
    public T get() {
        return getter.get();
    }

    @Nullable
    @Override
    public Object getDelegate() {
        return null;
    }

    @NotNull
    @Override
    public Getter<T> getGetter() {
        return null;
    }

    @Override
    public T invoke() {
        return null;
    }

    @Override
    public boolean isLateinit() {
        return false;
    }

    @Override
    public boolean isConst() {
        return false;
    }

    @NotNull
    @Override
    public String getName() {
        return null;
    }

    @NotNull
    @Override
    public List<KParameter> getParameters() {
        return null;
    }

    @NotNull
    @Override
    public KType getReturnType() {
        return null;
    }

    @NotNull
    @Override
    public List<KTypeParameter> getTypeParameters() {
        return null;
    }

    @Override
    public T call(Object... objects) {
        return null;
    }

    @Override
    public T callBy(Map<KParameter, ?> map) {
        return null;
    }

    @Nullable
    @Override
    public KVisibility getVisibility() {
        return null;
    }

    @Override
    public boolean isFinal() {
        return false;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public boolean isAbstract() {
        return false;
    }

    @NotNull
    @Override
    public List<Annotation> getAnnotations() {
        return null;
    }
}

But whenever I try to run that, I get the following: 但每当我尝试运行时,我会得到以下结果:

Error:(45, 12) java: reference to Getter is ambiguous 错误:(45,12)java:对Getter的引用不明确

both interface kotlin.reflect.KProperty0.Getter in kotlin.reflect.KProperty0 and interface kotlin.reflect.KProperty.Getter in kotlin.reflect.KProperty match 在kotlin.reflect.KProperty0中接口kotlin.reflect.KProperty0.Getter,在kotlin.reflect.KProperty中匹配接口kotlin.reflect.KProperty.Getter

The problematic function is this one : 有问题的功能是这一个

  @NotNull
  @Override
  public Getter<T> getGetter() {
      return null;
  }

And the relevant file is kotlin.reflect.KProperty.tk , you can find it here 相关文件是kotlin.reflect.KProperty.tk ,你可以在这里找到它

Any idea how could I solve it? 知道怎么解决呢?

Just specify which interface you mean: 只需指定您的意思:

public KProperty0.Getter<T> getGetter()

But I would prefer to implement the class in Kotlin and only consume it from Java. 但我更愿意在Kotlin中实现该类,并且只从Java中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM