[英]Kotlin: How can I use delegated properties in Java?
I know that you can't use the delegated property syntax in Java, and won't get the convenience of "overriding" the set/get operators as in Kotlin, but I'd still like to use an existing property delegate in Java. 我知道你不能在Java中使用委托属性语法,并且不会像在Kotlin中那样“覆盖”set / get运算符,但我仍然希望在Java中使用现有的属性委托。
For instance, a simple delegate of an int: 例如,int的简单委托:
class IntDelegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>) = 0
}
In Kotlin of course we can use this like so: 在Kotlin当然我们可以这样使用:
val x by IntDelegate()
But how can we use IntDelegate
in some form in Java? 但是我们如何在Java中以某种形式使用
IntDelegate
? This is the start, I believe: 这是开始,我相信:
final IntDelegate x = new IntDelegate();
And then using the functions directly. 然后直接使用这些功能。 But how can I use the
getValue
function? 但是我如何使用
getValue
函数呢? What do I pass for its parameters? 我的参数是什么? How do I get a
KProperty
for Java field? 如何获得
KProperty
for Java字段?
If you really want to know how Kotlin delegated property looks under the hood in Java, here it is: in this example a property x
of a java class JavaClass
is delegated to the Delegates.notNull
standard delegate. 如果你真的想知道Kotlin如何在Java中委托属性,那么它就是:在这个例子中,java类
JavaClass
的属性x
被委托给Delegates.notNull
标准委托。
// delegation implementation details
import kotlin.jvm.JvmClassMappingKt;
import kotlin.jvm.internal.MutablePropertyReference1Impl;
import kotlin.jvm.internal.Reflection;
import kotlin.reflect.KProperty1;
// notNull property delegate from stdlib
import kotlin.properties.Delegates;
import kotlin.properties.ReadWriteProperty;
class JavaClass {
private final ReadWriteProperty<Object, String> x_delegate = Delegates.INSTANCE.notNull();
private final static KProperty1 x_property = Reflection.mutableProperty1(
new MutablePropertyReference1Impl(
JvmClassMappingKt.getKotlinClass(JavaClass.class), "x", "<no_signature>"));
public String getX() {
return x_delegate.getValue(this, x_property);
}
public void setX(String value) {
x_delegate.setValue(this, x_property, value);
}
}
class Usage {
public static void main(String[] args) {
JavaClass instance = new JavaClass();
instance.setX("new value");
System.out.println(instance.getX());
}
}
However I wouldn't recommend to use this solution, not only because of the boilerplate required, but because it relies heavily on the implementation details of the delegated properties and kotlin reflection. 但是我不建议使用这个解决方案,不仅因为需要样板文件,而且因为它在很大程度上依赖于委托属性和kotlin反射的实现细节。
I know that you can't use the delegated property syntax in Java, and won't get the convenience of "overriding" the set/get operators as in Kotlin, but I'd still like to use an existing property delegate in Java.
我知道你不能在Java中使用委托属性语法,并且不会像在Kotlin中那样“覆盖”set / get运算符,但我仍然希望在Java中使用现有的属性委托。
No, just like what you said in start, it does not exist in Java. 不,就像你在开始时说的那样,它在Java中不存在。 But if you insist on doing it, you can do similar things.
但如果你坚持这样做,你可以做类似的事情。
public interface Delegate<T> {
T get();
void set(T value);
}
public class IntDelegate implements Delegate<Integer> {
private Integer value = null;
@Override
public void set(Integer value) {
this.value = value;
}
@Override
public Integer get() {
return value;
}
}
final Delegate<Integer> x = new IntDelegate();
Delcare x in interface allows you to have different implementation. Delcare x in界面允许您实现不同的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.