mirror of
https://github.com/fenixsoft/jvm_book.git
synced 2025-03-13 18:51:04 +08:00
42 lines
958 B
Java
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();
|
|
}
|
|
|
|
}
|