通过class实例取得类的接口,父类,构造器

interface China {
    public static final String NATIONAL = "JAPAN";
    public static final String AUTHOR = "SIGERU";

    public void sayJapan();

    public String sayHello(String name, int age);
}

public class PersonZ implements China {
    private String name;
    private int age;

    public PersonZ() {
    }

    public PersonZ(String name, int age) {
        this(name);
        this.setAge(age);
    }

    public PersonZ(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void sayJapan() {
        System.out.println("AUTHOR:" + AUTHOR + ",COUNTRY:" + NATIONAL);
    }

    @Override
    public String sayHello(String name, int age) {
        return name + ",Hello! I AM " + age + "NOW!";
    }

}

 

 

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

public class GetInterfaceDemo {
    public static void main(String[] args) {
        Class<?> c1 = null;
        String path = PersonZ.class.getName();
        try {
            c1 = Class.forName(path);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Class<?> c[] = c1.getInterfaces();
        Constructor<?> con[] = c1.getConstructors();
        for (int i = 0; i < c.length; i++) {
            System.out
                    .println("THe interface implemented is:" + c[i].getName());
        }
        for (int i = 0; i < con.length; i++) {
            Class<?> p[] = con[i].getParameterTypes();
            System.out.print("The constructor is:"
                    + Modifier.toString(con[i].getModifiers()) + " "     //使用modifier还原修饰符
                    + con[i].getName());
            System.out.print("(");
            for (int j = 0; j < p.length; j++) {
                System.out.print(p[j].getName() + "arg" + i);
                if (j < p.length - 1) {
                    System.out.print(",");
                }
            }
            System.out.println("){}");
        }
        System.out.println("The superclass extended is:"
                + c1.getSuperclass().getName());
    }
}

你可能感兴趣的:(Class)