Код: Выделить всё
interface Parent {
static int hi() { return 1; }
}
class Child implements Parent {
static int hi() { return 0; }
}
class Child2 implements Parent {
static int hi() { return 3; }
}
class Connector {
int getHi() { return T.hi(); }
}
class Main {
public static void main(String[] args) {
Connector c = new Connector();
System.out.println(c.getHi()); // want it to print 0, but prints 1
Connector c2 = new Connector();
System.out.println(c2.getHi()); // want it to print 3, but prints 1
}
}
Код: Выделить всё
interface Parent {}
class Child implements Parent {
static int hi() { return 0; }
}
class Child2 implements Parent {
static int hi() { return 3; }
}
class Connector {
static int getHi(Class classType) {
try {
return (int) MethodHandles.lookup().findStatic(classType, "hi", MethodType.methodType(int.class)).invokeExact();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
class Main {
public static void main(String[] args) {
System.out.println(Connector.getHi(Child.class)); // prints 0
System.out.println(Connector.getHi(Child2.class)); // prints 3
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... eric-types