jvm_book/src/org/fenixsoft/jvm/chapter8/GrandFatherTestCase_1.java
ZhouZhiming 10c303de60 init
init
2019-12-24 11:11:01 +08:00

42 lines
958 B
Java

package org.fenixsoft.jvm.chapter8;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import static java.lang.invoke.MethodHandles.lookup;
/**
* @author zzm
*/
public class GrandFatherTestCase_1 {
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() {
try {
MethodType mt = MethodType.methodType(void.class);
MethodHandle mh = lookup().findSpecial(GrandFather.class,
"thinking", mt, getClass());
mh.invoke(this);
} catch (Throwable e) {
}
}
}
public static void main(String[] args) {
(new GrandFatherTestCase_1().new Son()).thinking();
}
}