package com.huawei.pia;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Person extends SuperClass1 implements Inter1,Inter2 {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public Person(){};
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 String toString() {
// TODO 自动生成的方法存根
return "[" + this.name + ":" + this.age + "]";
}
static class Hello{
public static void main(String[] args) {
Class<?> demo = null;
try {
demo = Class.forName("com.huawei.pia.Person");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field[] field = demo.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
//得到修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
//属性类型
Class<?> type = field[i].getType();
System.out.println("修饰符: "+priv+",\n属性类型: "+type+",\n属性名: "+field[i].getName());
}
//得到构造方法
Constructor<?>[] cons = demo.getConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println("构造方法: "+cons[i].getName());
}
//得到接口
Class<?> inter[] = demo.getInterfaces();
for (int i = 0; i < inter.length; i++) {
System.out.println("接口"+(i+1)+": "+inter[i].getName());
}
//得到父类
Class<?> superClass = demo.getSuperclass();
System.out.println("父类: "+superClass.getName());
}
}
}