62,614
社区成员
发帖
与我相关
我的任务
分享
import java.lang.invoke.MethodHandle;
import static java.lang.invoke.MethodHandles.lookup;
import java.lang.invoke.MethodType;
public class Test {
class GrandFather{
void thinking ()
{
System.out.println("I am grandfather");
}
}
class Father extends GrandFather{
void thinking ()
{
System.out.println("I am father ");
}
}
class Son extends Father{
void thinking ()
{
// System.out.println("I am son");
try {
MethodType mt = MethodType.methodType(void.class);
MethodHandle mh = lookup().findSpecial(GrandFather.class, "thinking",mt , getClass());
mh.invoke(this);
} catch (Throwable e) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
(new Test().new Son()).thinking();
}
}
MethodType mt = MethodType.methodType(void.class);
MethodHandle mh = lookup().findVirtual(GrandFather.class,"think",mt).bindTo(new GrandFather());
mh.invokeExact();