java中的字节码

package chapter03;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test04 {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        Parent parent = new child();
        //类对象
        Class aClass = parent.getClass();


        //获取类的名称
        System.out.println(aClass.getName());
        System.out.println(aClass.getSimpleName());
        System.out.println(aClass.getPackageName());

        //获取类的父类
        Class superclass = aClass.getSuperclass();
        System.out.println(superclass);

        //获取类的接口
        Class[] interfaces = aClass.getInterfaces();
        System.out.println(interfaces.length);

        //获取类的属性
        try{
            Field f = aClass.getField("test1"); //public
            System.out.println("test1");
        }
        catch (NoSuchFieldException e)
        {
            System.out.println("no such method");
        }
        try{
            Field f2 =   aClass.getDeclaredField("test2");
        }
        catch (NoSuchFieldException e)
        {
            System.out.println("no such method");
        }

        Field[] fields =   aClass.getDeclaredFields();
        Method method = aClass.getMethod("test1");
        System.out.println(method);
        Method method1 = aClass.getDeclaredMethod("test2");
        System.out.println(method1);
   
        try{
            Constructor constructor = aClass.getConstructor();
        }
        catch( NoSuchMethodException e)
        {

        }
                
        Constructor[] constructors = aClass.getConstructors();
        Constructor declaredConstructor = aClass.getDeclaredConstructor();

        int modifiers = aClass.getModifiers();
        boolean aPrivate = Modifier.isPrivate(modifiers);
        System.out.println(aPrivate);
    }
}
class Parent{
    public void test1() {
        System.out.println("test1...");
    }
};
class  child extends  Parent{
    public void test2()
    {
        System.out.println("test2....");
    }
}

你可能感兴趣的:(Java基础,java,开发语言)