package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Apple extends Fruits {

    public Apple() {
        System.out.println("Constructor: Apple");
    }

    @Override
    void getFruitColor() {
        super.getFruitColor();
        System.out.println("RED");
    }

    @Override
    void getFruitName() {
        super.getFruitName();
        System.out.println("Apple");
    }
}

class doReflect{ public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { // 1. Obtain Class <? > clazz = Class.forName("reflect.Apple");
        System.out.println("Get class:"+clazz); Constructor = clazz.getconstructor (); System.out.println("Get structure:"+constructor); Object bean = clazz.newinstance (); // System.out.println(bean.tostring ()) must be instantiated; [] methods = bean.getClass().getDeclaredmethods (); System.out.println("---------getDeclaredMethods");
        for(Method m : methods) { System.out.println(m); } / / void reflect. Apple. GetFruitColor () / / void reflect. Apple. GetFruitName () / / 3.2 get Method [] methods2 = bean.getClass().getMethods(); System.out.println("---------getMethods");
        for(Method m : methods2) { System.out.println(m); } // ---------getMethods // public final void java.lang.Object.wait() throws java.lang.InterruptedException // public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException // public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException // public boolean java.lang.Object.equals(java.lang.Object) // public java.lang.String java.lang.Object.toString() // public native int java.lang.Object.hashCode() // public final native java.lang.Class java.lang.Object.getClass() // public final native Void Java. Lang. Object. Notify () / / public final native void Java lang. Object. The notifyAll () / 3.3 / call Method Method = bean.getClass().getDeclaredMethod("getFruitColor");
        System.out.println("Call method:"); method.invoke(bean); }}Copy the code